954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

read a array of type struct

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.

mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.

mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

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;
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

u the man.

Thanks,

mrjoli021
Junior Poster
172 posts since Mar 2007
Reputation Points: 7
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You