files using getline

so let say i have a struct and array i need use getline

for example
text file 1 student
0 bob F
1 mike B
2 sandy C
text file 2 gpa file
2 math 2
1 history 3
0 english 0
text file 3
2 pass 213 jog rd, 23478 ,new york
0 failed 745 starship rd,25698,new york
1 pass 112 nice rd,23958,earth
so i understand the first two files but how would get line fall into play on the last file
would i need to use '=='
Here's the getline syntax:
 
std::getline(your_file, variable_to_save_the_line_in, line_separator)


And here's how you transfer all the content of the file line by line into a vector.

1
2
3
4
5
std::string line;
std::vector<string> all_lines;

while(std::getline(file,line,'\n'))
 all_lines.push_back(line);
You'd have to give more information about the struct (for e.g. its declaration) corresponding to text file 3 to receive any meaningful suggestions about how to parse the file to 'fill-up' the matching data-members of this underlying struct from the lines of the file

PS: since you did not have any questions re file 1 and file 2 it was unnecessary to post them in the first place
Text file 3 apparently looks like this:
2 pass   213 jog rd, 23478 ,new york
0 failed 745 starship rd,25698,new york
1 pass   112 nice rd,23958,earth

(formatted with the [output] [/output] tags).

The first two fields presumably could be interpreted as an id number followed by a fixed-length character string. The next field, a 3-digit integer could be some sort of score, or maybe it is part of what looks like an address?
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    ifstream fin3("data3.txt");
    
    int id;
    string rating;
    string address;
    
    cout << setw(10) << "id"
         << setw(10) << "rating"
         << "    address\n\n";
         
    while (fin3 >> id >> rating >> ws && getline(fin3, address))
    {
        cout << setw(10) << id
             << setw(10) << rating
             << "    "   << address
             << "\n"; 
    }

}

        id    rating    address

         2      pass    213 jog rd, 23478 ,new york
         0    failed    745 starship rd,25698,new york
         1      pass    112 nice rd,23958,earth
Last edited on
Topic archived. No new replies allowed.