Jan 17, 2022 at 7:15am UTC
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 Jan 17, 2022 at 7:16am UTC
Jan 17, 2022 at 8:06am UTC
If you only want to print the file then you read the file line by line and replace , with \t.
Jan 17, 2022 at 8:32am UTC
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 Jan 17, 2022 at 8:33am UTC
Jan 24, 2022 at 7:02am UTC
@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 Jan 24, 2022 at 7:37am UTC
Jan 24, 2022 at 9:47am UTC
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 Jan 24, 2022 at 10:09am UTC
Jan 24, 2022 at 10:11am UTC
@seeplus
So the whole concept is about file processing with ofstream() right?
Jan 24, 2022 at 11:37am UTC
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 Jan 24, 2022 at 1:14pm UTC