| | |
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: 939 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C
#include adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc directory dynamic fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware highest homework inches incrementoperators kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc process program programming radix recursion recv repetition research scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation threads turboc unix user variable visualstudio voidmain() wab win32 windows.h






