| | |
read a array of type struct
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Mar 2007
Posts: 156
Reputation:
Solved Threads: 0
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.
students is a global variable.
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.
Last edited by Ancient Dragon; Apr 10th, 2007 at 1:33 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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.
C Syntax (Toggle Plain Text)
infile >> students[i].name >> students[i].address;
Last edited by Ancient Dragon; Apr 10th, 2007 at 3:25 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Array in struct (C#)
- Problem in Reading Array Variable (C)
- Read from an array (Java)
Other Threads in the C Forum
- Previous Thread: While not ended
- Next Thread: strpbrk
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab win32api windows.h






