C++ and files

Hello to everyone. Your site is treasure of knowledge! Thanks for all the help that you provide me over the years.

I have a problem with C++ that i cannot solve.

I have a C++ code that saves some information in a txt file in this format

1
2
A2334, Cyprus, Arrival, 12 : 13 : 12, 0, 0 : 0 : 0
C3948, London, Departure, 12 : 13 : 43, 0, 0 : 0 : 0


Now i want to extract the string up until the comma and then put it in a link list in order to modify it later. Is like a load function.

So i want to read the string until the comma and save it.
For example the first string
A2334

i want it to be saved in temp->entry.FlightNo
the second in temp->entry.Destination
and so on

But how i am going to do that? I am searching the file character by character and i am saving until the EOF but to get the string i am using the getline function.
This is not good because i have the while loop to end when the EOF is reached so each time the getline is getting place and i always get the last line and not all of them.

Can i read words/strings from a file that has multiple lines?
Or can i specify what line i am with a counter?
Any ideas?

Thanks in advance
To read line by line use :
http://www.cplusplus.com/reference/iostream/istream/getline/

To parse the line use :
http://www.cplusplus.com/reference/string/string/find/
http://www.cplusplus.com/reference/string/string/substr/
(and other stuff, be creative)

Here is how you want to do it :
1
2
3
4
5
std::ifstream file("in.txt");
std::string line;
while(std::getline(file, line)) {
    //parse line
}
Topic archived. No new replies allowed.