c++fstreamsetw

How to get value for each attribute when file was save with setw() function


I'm a newbie of C++. I'm sorry if my question have any errors.

I have a file with 4 attributes: Name, Position, Birthday, Salary

And i use setw() function to save file.txt with format like this

NAME                POSITION        BIRTHDAY        SALARY
James Smith         CEO             10/12/1991      3000
Robert Wiliam       IT              5/4/1990        4999
Maria Rodriguez     Designer        12/3/1994       4923
Maria Hernandez     Waiter          22/2/1992       4022

Now, i want to read back all of them for each of attribute is each value of this table like this (example for a person):

staff1:
Name: James Smith 
Position: CEO
Birthday: 10/12/1991
Salary: 3000

I have no idea for getting value foreach Staff. Can you give me some ideas?

If each atttribute was saved and separate between them is comma, i'll use

getline(fileIn, name, ',');

but it's not work in this case.


Solution

  • Use fileIn.getline(name, 20) (assuming 20 spaces set using setw when saving)

    Then use
    fileIn >> position >> birthday >> salary to read in the rest.

    If you don't know if the remaining columns may contain spaces or not, you can continue and use getline instead of the extraction operator.