944,201 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 3182
  • C RSS
Apr 10th, 2007
0

read a array of type struct

Expand Post »
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.


Similar Threads
Reputation Points: 7
Solved Threads: 0
Junior Poster
mrjoli021 is offline Offline
170 posts
since Mar 2007
Apr 10th, 2007
0

Re: read a array of type struct

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.
Last edited by Ancient Dragon; Apr 10th, 2007 at 1:33 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Apr 10th, 2007
0

Re: read a array of type struct

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.
Reputation Points: 7
Solved Threads: 0
Junior Poster
mrjoli021 is offline Offline
170 posts
since Mar 2007
Apr 10th, 2007
0

Re: read a array of type struct

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.
  1. infile >> students[i].name >> students[i].address;
Last edited by Ancient Dragon; Apr 10th, 2007 at 3:25 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Apr 10th, 2007
0

Re: read a array of type struct

u the man.

Thanks,
Reputation Points: 7
Solved Threads: 0
Junior Poster
mrjoli021 is offline Offline
170 posts
since Mar 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: While not ended
Next Thread in C Forum Timeline: strpbrk





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC