Reputation: 553
let say that i have a file
ID Name Month Actual Quantity Desired Quantity Cost
2 pepsi 1 1 3.3
3 pepsi 2 3 5.3
how would i read that into a vector called vector<Item> items
; where Item is a class that consists of those names as listed in the list. i have a set() methods for all of them but how would i read one of each values and set it for example setID() and its value and if it is empty put a value -1. What i have so far is just a basic file open
char file_name[81];
cout<<"Enter a file to open (ex: file.txt): ";
cin.ignore();
flush(stdin);
cin.getline(file_name, 81);
ifstream input(file_name);
should i use a istringstream or what?
UPDATE:
char file_name[81];
cout<<"Enter a file to open (ex: file.txt): ";
cin.ignore();
cin.getline(file_name, 81);
ifstream input(file_name);
string line;
getline(input,line);
while (getline(input,line)){
BUT I get an error: statement cannot resolve address of overloaded function at line : ifstream input(file_name)
Upvotes: 2
Views: 173
Reputation: 153792
They way I would do is is to start writing a suitable input operator:
std::istream& operator>> (std::istream& in, Item& item) {
...
}
Once this operator is in place, you can read the file using something like this:
std::vector<Item> items;
input.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::copy(std::istream_iterator<Item>(input), std::istream_iterator<Item>(),
std::back_inserter(items));
Upvotes: 1