trouble reading from a text file

hello everyone
I tried to read in a file that contain studentId(8 integer long) and GPA in the same line separated by a comma, but I couldn't. Any help is appriciated
example: 145453565, 4.0
34344443, 3.9
23454345, 3.4
12345678, 3.4

void studentRecord::loading(string filename)
{
ifstream infile;
int studentId;
double GPA;

string temp;
infile.open(filename.c_str());

if(!infile.is_open() )
{
cout << "ERROr" << endl;
return;
}
while(!infile.eof())
{
getline(infile, temp);
int pos = temp.find(',');
temp.substr(0, pos);
temp >> studentId;
temp.substr(pos, temp.size())
temp >> GPA;

}

}
Last edited on
Please use code tags.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

1
2
3
4
5
6
7
8
9
10
stringstream ss;

while(getline(infile, temp, ',') && getline(infile, temp2))
 {
    ss << temp;
    ss >> studentId;
    ss << temp2;
    ss >> GPA;
 
}
Last edited on
thank you
hi, I tried your code and it seems like it only read one top of the line
my file contains
24543544, 4.0
43523454, 3.9
and the output is 24543455 4.0
Topic archived. No new replies allowed.