| | |
structure and reading a text file
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jan 2009
Posts: 3
Reputation:
Solved Threads: 0
hi,
how can i read a text file "textfile.txt" with a structure? (in C)
thanks
the structure is in this form.
how can i read a text file "textfile.txt" with a structure? (in C)
thanks
the structure is in this form.
c Syntax (Toggle Plain Text)
typedef struct list { char ID[9]; char YR[10]; char lname[14]; char fname[24]; }LIST[256];
Last edited by Ancient Dragon; Jan 7th, 2009 at 10:34 am. Reason: add code tags
Don't know the answer to your question until I know the contents of the text file. Without that knowledge, generally I would use fgets() to read a line, then parse it into its individual parts into the structure.
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.
you could do something like this:
Of course you will have to put the above in a loop after opening the file for reading.
C Syntax (Toggle Plain Text)
fscanf(fp,"%s%s%s", List[i].ID, List[i].YR, List[i].lname); // get the first name separately because it may be composed of // two or more names with spaces, so fscanf() will not work with this. fgets(List[i].fname, sizeof(List[i].fname, fp);
Of course you will have to put the above in a loop after opening the file for reading.
Last edited by Ancient Dragon; Jan 7th, 2009 at 12:04 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
- Reading in a *.csv file and loading the data into an Array (Java)
- Reading Data from text file (C++)
- Dynamic array (C++)
- Problem with strtok() for data structure creation (C++)
- dynamically update pages (HTML and CSS)
- read text file (C)
- struct linked list problem (C)
- header file/ classes (C++)
- How to delete a data structure in Builder 6.0? (C++)
- file input and output (C)
Other Threads in the C Forum
- Previous Thread: Problem with array of pointers
- Next Thread: Wireless networks
Views: 938 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C
* api append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax database directory drawing dynamic executable execv feet fgets file floatingpointvalidation fork frequency function functions getlogicaldrivestrin givemetehcodez global graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked reversing scheduling segmentationfault send single socketprogramming spoonfeeding standard strchr string student suggestions system test testautomation testing unix urboc user whythiscodecausesegmentationfault win32api windowsapi






