I have a csv file that I need to be able to read it into a struct array. I have read examples and they all match what I have.

void ReadStudents ()
{
fstream infile;
int i;

infile.open ("c:\\students.txt");
for (i =0; i < 20; i++)
{
infile >> ::students[i];
}
infile.close ();
}

students is a global variable.


You failed to say what the problem is, but if you want to read one column at a time then using the extraction operator like that won't work right with csv files because columna may, or may not, be separated by white space (spaces and/or tabs). The csv files I've worked with had a comma between each column. So you would probably want to use the getline() function so that you can specify the character that terminates a word.

the file is tab separated like you said, but the problem is I get the error:

"error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'StudentType'"

at compile time.

If you want to read the file like that you will have to write an overloaded extraction operator for your structure because the standard stream classes don't know how to read structures. Or if you don't want to do that, then extract each element individually, something like this: I don't know what the structure looks like so I'll just make up something.

infile >> students[i].name >> students[i].address;

u the man.

Thanks,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.