Processing an input file

closed account (oEwqX9L8)
I'm writing a program that takes in a file in the format:
id number
name
address
phone number

The input file will repeat this set of four lines an unspecified number of times. I have a class with a member variable for each item and I want to put each instance of the class into a vector. I'm struggling to come up with a way to put each line into a different variable(int, string, string, string) and then keep repeating that for every four lines. My first idea was to use a stringstream but I couldn't get anywhere with that.

I use this to read in each line in the file.
1
2
3
  while (getline(studentFile,line)) {
        
    }


Can anyone give me some ideas to work with? Thanks for your help!
Is the file format fixed, or can you change it?

If you are able to change the format to something like: "id number,name,address,phone number" you could then use getline() to read the entire line, then use a stringstream to parse this line.

1
2
3
4
5
6
7
8
while(getline(studentFile, line))
{
   stringstream sin(line);
   getline(sin, id_number, ',');
   getline(sin, name, ',');
   getline(sin, address, ',');
   getline(sin, phone_number);
}


I also recommend that you treat the Id number as a string, since an ID number is not really a number.

Topic archived. No new replies allowed.