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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <cstring>
using namespace std;
typedef struct cus {
string Name;
string Sex;
string ID;
string Address;
} CUS;
int main()
{
int nCus = 0;
vector<CUS> list;
fstream fi;
string line = "";
char *token = nullptr;
// Open input file
fi.open("c:\\input.csv", ios::in);
// Check if file is open, if not, exit from main
if (!fi.is_open()) {
return -1;
}
// Get the first line where the count of entries found
getline(fi, line);
// Print line read from file for debugging
cout << "Line read from file: " << line << endl;
// Get the string count value
token = strtok((char *)line.c_str(), ",");
// Check first if token is not null before using to avoid problems
if (token != nullptr) {
// Convert string count into an int count
nCus = atoi(token);
}
//Print the count of customer for checking for debugging
cout << "Number of entries in input file: " << nCus << endl;
// Get each name entry and put into a structure then add the structure into a vector
for (int i = 0; i < nCus; i++) {
// Allocate memory for a name structure
CUS *p = new CUS;
// Read name entry from file
getline(fi, line);
// Print line read from file for debugging
cout << "\nLine read from file: " << line << endl;
// Break this line into tokens to get each info
// This first token is the name, save it if its not null
token = strtok((char *)line.c_str(), ",");
if (token != nullptr) {
string name(token); //Convert char * to string
p->Name = name;
}
else {
p->Name = ""; //Set an empty string if token for name is null
}
// Second token is sex ('0' for male ad '1' for female)
// Put "Male" to struct if value read is '0', otherwise, 'Female'
token = strtok(nullptr, ",");
if (token != nullptr) {
if (strcmp(token, "0") == 0) { //If token is '0', sex is "Male"
p->Sex = "Male";
}
else {
p->Sex = "Female"; // If not '0' then sex is "Female"
}
}
else {
p->Sex = ""; //Set an empty string if token for sex is null
}
// Third token is the ID, save it if its not null
token = strtok(nullptr, ",");
if (token != nullptr) {
string ID(token); //Convert char * to string
p->ID = ID;
}
else {
p->ID = ""; //Set an empty string if token for ID is null
}
// Fourth token is the Address, save it if its not null
token = strtok(nullptr, ",");
if (token != nullptr) {
string address(token); //Convert char * to string
p->Address = address;
}
else {
p->Address = ""; //Set an empty string if token for address is null
}
//Print all the name entry information found for tracing/debugging
cout << "\nDisplaying name " << i + 1 << " information..." << endl;
cout << "Name: " << p->Name << "\nSex: " << p->Sex << "\nID: " << p->ID << "\nAddress: " << p->Address << endl;
//Now save this name entry info into a vector for further manipulation
list.push_back(*p);
}
//Outside this loop, you can add whatever code you want if there is a need to manipulate or use the vector/list that contains all the input name entries.
return 0;
}
|