Hello there,
I'm reading lines from a text file in C++ which contains integer + string + float number(like 3,67 with comma) + string in this order. I need the float number to sort the lines but I couldn't manage to separate the data into the types I can use so far. I tried different kind of functions and the best I could do was such a code;
This fails at reading the floating number which has comma in it and then last string is read as string starting with the comma and rest of the number. An output example is:
698 John 3 ,67
It doesn't read last string on the line as well. I understand that part but simply I need another read but what I want exactly is to separate one line using "tab" as a seperator into proper data types and then using the numbers as integers, and the grades as floating numbers. How Can I do this?
I'm reading lines from a text file in C++ which contains integer + string + float number(like 3,67 with comma) + string in this order. I need the float number to sort the lines but I couldn't manage to separate the data into the types I can use so far. I tried different kind of functions and the best I could do was such a code;
Code:
void main (){
ifstream records;
records.open("records.txt");
int id;
string line;
char name[100];
float gpa;
string depart;
if (records.is_open()) {
while (!records.eof()) {
getline(records, line);
istringstream deneme(line);
deneme >> id;
deneme >> name;
deneme >> gpa;
deneme >> depart;
// cout << id << " -- " << name << " -- " << gpa1 << " -- " << depart << " -- " << endl;
}
}
records.close();
system("PAUSE");
}
698 John 3 ,67
It doesn't read last string on the line as well. I understand that part but simply I need another read but what I want exactly is to separate one line using "tab" as a seperator into proper data types and then using the numbers as integers, and the grades as floating numbers. How Can I do this?
Comment