How to convert this txt file to an table output

I have a "New.txt" file with this data in it :

userchoice_for_vehicle,vehicle_No,manfacture,in_time
1,1122,BMW,Mon Jan 17 11:50:00 2022
2,2233,Honda,Mon Jan 17 10:57:09 2022
2,3344,Ducati,Mon Jan 17 09:57:08 2022
1,4455,Hyundai,Mon Jan 17 11:54:10 2022
2,5566,Hero,Mon Jan 17 12:57:00 2022
1,6677,Jeep,Mon Jan 17 14:57:00 2022
1,7788,AUDI,Mon Jan 17 15:57:00 2022

I want to print all of this data in a table format in an output console.
Please help.
Last edited on
What is "table format"?
If you only want to print the file then you read the file line by line and replace , with \t.
No, I cannot replace it with /t.

I just have to print this file format as:

userchoice_for_vehicle    vehicle_No   manufacture   in_time
1                                   1122           BMW              Mon Jan 17 11:50:00 2022
2                                   2233           Honda            Mon Jan 17 10:57:09 2022
2                                   3344           Ducati            Mon Jan 17 09:57:08 2022
1                                   4455           Hyundai         Mon Jan 17 11:54:10 2022
2                                   5566           Hero              Mon Jan 17 12:57:00 2022
1                                   6677           Jeep              Mon Jan 17 14:57:00 2022
1                                   7788           AUDI             Mon Jan 17 15:57:00 2022


I need this printed in the console.
Last edited on
Perhaps something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>

int main() {
	constexpr size_t widths[] {24, 12, 13};
	std::ifstream ifs("New.txt");

	if (!ifs)
		return (std::cout << "Cannot open file\n"), 1;

	for (std::string line; std::getline(ifs, line); ) {
		std::istringstream ifs(line);
		size_t cnt {};

		for (std::string elem; std::getline(ifs, elem, ','); ) {
			if (cnt < std::size(widths))
				std::cout.width(widths[cnt++]);

			std::cout << std::left << elem;
		}

		std::cout << '\n';
	}
}



userchoice_for_vehicle  vehicle_No  manfacture   in_time
1                       1122        BMW          Mon Jan 17 11:50:00 2022
2                       2233        Honda        Mon Jan 17 10:57:09 2022
2                       3344        Ducati       Mon Jan 17 09:57:08 2022
1                       4455        Hyundai      Mon Jan 17 11:54:10 2022
2                       5566        Hero         Mon Jan 17 12:57:00 2022
1                       6677        Jeep         Mon Jan 17 14:57:00 2022
1                       7788        AUDI         Mon Jan 17 15:57:00 2022

@seeplus so is this for file processing, something like sequential file of .txt or .dat?

Also how to make something like delete a record for a vehicle_no and lets you update any information in the file in terms of a beginner level for something like this?

A case will be great to show all printed records as a table format on the output, delete a record for a vehicle_no and update any information in the file.

Last edited on
If you need to modify the data in a text file, the usual way to do this is to read/parse the data into an appropriate data structure, update the data as appropriate and then re-write the file from the data. You can't easily insert/delete data in the middle of a text file.

A possible data struct for the file data could be something like:

1
2
3
4
5
6
struct Vehicle {
    unsigned choice {};
    unsigned vehNo {};
    std::string manufact;
    std::string in_time;
};


and then use say a std::vector<Vehicle> to hold the file data.
Last edited on
@seeplus

So the whole concept is about file processing with ofstream() right?
As a starter, this will read the file into a vector and then display the read data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
#include <vector>
#include <iterator>

struct Vehicle {
	unsigned choice {};
	unsigned vehNo {};
	std::string manufact;
	std::string in_time;
};

constexpr size_t widths[] {24, 12, 13};

static_assert(std::size(widths) >= 3);

std::istream& operator>>(std::istream& is, Vehicle& veh) {
	char com {};

	(is >> veh.choice >> com >> veh.vehNo >> com) && std::getline(is, veh.manufact, ',') && std::getline(is, veh.in_time);
	return is;
}

std::ostream& operator<<(std::ostream& os, const Vehicle& veh) {
	return os << std::left << std::setw(widths[0]) << veh.choice << std::setw(widths[1]) << veh.vehNo << std::setw(widths[2]) << veh.manufact << veh.in_time << '\n';
}

auto getHeader(std::istream& is) {
	std::string line;
	std::vector<std::string> cols;

	if (std::getline(is, line)) {
		std::istringstream iss(line);

		for (std::string head; std::getline(iss, head, ','); cols.emplace_back(std::move(head)));
	}

	return cols;
}

void showHeader(const std::vector<std::string>& header) {
	for (size_t cnt {}; cnt < header.size(); ++cnt) {
		if (cnt < std::size(widths))
			std::cout.width(widths[cnt]);

		std::cout << std::left << header[cnt];
	}

	std::cout << '\n';
}

int main() {
	std::ifstream ifs("New.txt");

	if (!ifs)
		return (std::cout << "Cannot open file\n"), 1;

	std::vector<Vehicle> vehicles;
	const auto header {getHeader(ifs)};

	for (Vehicle v; ifs >> v; vehicles.emplace_back(std::move(v)));
	showHeader(header);

	for (const auto& v : vehicles)
		std::cout << v;

	std::cout << '\n';
}



userchoice_for_vehicle  vehicle_No  manfacture   in_time
1                       1122        BMW          Mon Jan 17 11:50:00 2022
2                       2233        Honda        Mon Jan 17 10:57:09 2022
2                       3344        Ducati       Mon Jan 17 09:57:08 2022
1                       4455        Hyundai      Mon Jan 17 11:54:10 2022
2                       5566        Hero         Mon Jan 17 12:57:00 2022
1                       6677        Jeep         Mon Jan 17 14:57:00 2022
1                       7788        AUDI         Mon Jan 17 15:57:00 2022

Last edited on
As an extension, this will read the file, display the data and then re-write the file from the data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
#include <vector>
#include <iterator>

struct Vehicle {
	unsigned choice {};
	unsigned vehNo {};
	std::string manufact;
	std::string in_time;
};

constexpr size_t widths[] {24, 12, 13};

static_assert(std::size(widths) >= 3);

std::istream& operator>>(std::istream& is, Vehicle& veh) {
	char com {};

	(is >> veh.choice >> com >> veh.vehNo >> com) && std::getline(is, veh.manufact, ',') && std::getline(is, veh.in_time);
	return is;
}

std::ostream& operator<<(std::ostream& os, const Vehicle& veh) {
	return os << std::left << std::setw(widths[0]) << veh.choice << std::setw(widths[1]) << veh.vehNo << std::setw(widths[2]) << veh.manufact << veh.in_time << '\n';
}

std::ostream& operator<<(std::ostream& os, const std::vector<Vehicle>& veh) {
	for (const auto& v : veh)
		os << v.choice << ',' << v.vehNo << ',' << v.manufact << ',' << v.in_time << '\n';

	return os;
}

std::ostream& operator<<(std::ostream& os, const std::vector<std::string>& head) {
	for (size_t e = 0; const auto& elem : head) {
		if (e++)
			os << ',';

		os << elem;
	}

	return os;
}

std::istream& operator>>(std::istream& is, std::vector<std::string>& cols) {
	std::string line;

	cols.clear();

	if (std::getline(is, line)) {
		std::istringstream iss(line);

		for (std::string head; std::getline(iss, head, ','); cols.emplace_back(std::move(head)));
	}

	return is;
}

void showHeader(const std::vector<std::string>& header) {
	for (size_t cnt {}; cnt < header.size(); ++cnt) {
		if (cnt < std::size(widths))
			std::cout.width(widths[cnt]);

		std::cout << std::left << header[cnt];
	}

	std::cout << '\n';
}

int main() {
	std::ifstream ifs("New.txt");

	if (!ifs)
		return (std::cout << "Cannot open file\n"), 1;

	std::vector<Vehicle> vehicles;
	std::vector<std::string> header;

	ifs >> header;

	for (Vehicle v; ifs >> v; vehicles.emplace_back(std::move(v)));

	showHeader(header);

	for (const auto& v : vehicles)
		std::cout << v;

	std::cout << '\n';
	ifs.close();

	std::ofstream ofs("New.txt");

	ofs << header << '\n';
	ofs << vehicles;
}

Topic archived. No new replies allowed.