I've used C# and Java, so I'm used to opening an input file and reading until the end of the file stream while using some sort of .split
on each line with the delimiter as a tab, comma, etc. to get the individual values OR extracting the a specific number of characters for each value. I recently began taking a C++ class and haven't had any problems with the assignments so far because they were mostly using console input/output, but I'm finding it rather difficult to extract data from a file:
struct Student{
string _fullName[50];
int _grade;
float _avg;
};
int main(){
Student students[30];
ifstream inputFile;
inputFile.open("file.txt");
if (inputFile.is_open()){
int i = 0;
char data[100];
while (inputFile.getline(data, 100)){
//need to split the data by \t and properly convert it into an int/string/float where needed
}
}
system("pause");
return 0;
}
I considered changing the getline delimiter to \t and setting each value that way (i.e., resetting i to 0 after the 3 properties have been set), but this won't work if one of the lines is missing a value. That won't matter in this small assignment, but I'm looking to do this correctly and efficiently. What's the most efficient way to extract the data on each line and correctly parse it into the appropriate data type?
Note: I'm not supposed to be using vectors or algorithms at this point.