| | |
Text file parser in C
![]() |
•
•
Join Date: Apr 2007
Posts: 9
Reputation:
Solved Threads: 0
Hi, I have a text file which looks like this:
3Com 3C996B Gigabit Server NIC
NEC PCI to USB Enhanced Host Controller (B1)
....
....
Now, I need a 'C' Program which will copy each line into an array of strings or character array, such as char* Cards[10] . Could any body pls help me out?
Thanks in advance,
- Vijay
3Com 3C996B Gigabit Server NIC
NEC PCI to USB Enhanced Host Controller (B1)
....
....
Now, I need a 'C' Program which will copy each line into an array of strings or character array, such as char* Cards[10] . Could any body pls help me out?
Thanks in advance,
- Vijay
use FILE and associated functions in stdio.h. First call fopen() to open the file, then in a loop call fgets() to read each line. After end-of-file is reaches call fclose() to close the file.
If you use an array of char pointers as you posted you will want to call malloc() to allocate memory for the pointer before copying the line into the array.
If you use an array of char pointers as you posted you will want to call malloc() to allocate memory for the pointer before copying the line into the array.
Last edited by Ancient Dragon; Apr 26th, 2007 at 7:35 am.
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.
•
•
Join Date: Apr 2007
Posts: 9
Reputation:
Solved Threads: 0
I have the following sample.
The above program displays the following output.
3Com 3C996B Gigabit Server NIC
NEC PCI to USB Enhanced Host Controller (B1)
But, I want to read those lines into an array of strings, which size is vary depends on the no.of lines. pl suggest me how to achieve this.
Thanks in advance,
- Vijay
c Syntax (Toggle Plain Text)
int main() { FILE * pFile; char string [100]; // char filename = "c:\\cards.txt"; pFile = fopen ("c:\\cards.txt" , "r"); if (pFile == NULL) perror ("Error opening file"); else { while(fgets (string , 100 , pFile)) printf("%s",string); fclose (pFile); } return 0; }
3Com 3C996B Gigabit Server NIC
NEC PCI to USB Enhanced Host Controller (B1)
But, I want to read those lines into an array of strings, which size is vary depends on the no.of lines. pl suggest me how to achieve this.
Thanks in advance,
- Vijay
Last edited by Ancient Dragon; Apr 26th, 2007 at 9:51 am. Reason: add code tags removed color tags
that's a pretty good start. have you learned linked lists yet? if you have, then use a linked list to store the strings. If not, then just use a simple array of pointers as in your previous post. Just make an array large enough, or you can use malloc() and realloc() to dynamically increate the array size as needed while reading the file.
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.
•
•
Join Date: Apr 2007
Posts: 9
Reputation:
Solved Threads: 0
I did it in the following way.
But instead of setting up a fixed length array, pls suggest me the steps to create a dynamic array using malloc(), or realloc().
c Syntax (Toggle Plain Text)
int main() { FILE * pFile; char string [10][100]; int i=0,n=0; pFile = fopen ("c:\\cards.txt" , "r"); if (pFile == NULL) perror ("Error opening file"); else { while(fgets (string[i] , 100 , pFile)) { printf("%s",string[i]); i++; } fclose (pFile); } i=n; for(i=0;i<n;i++) printf("%s",string[i]); return 0; }
Last edited by WaltP; May 2nd, 2007 at 3:17 am. Reason: Please learn to use CODE tags -- read the announcements at the top of the forum
If you know how to use pointers then you can do this:
You will put the last few lines inside the loop that reads the file. Use another integer to count the number of lines that have been read. When that counter is the same as arraySize in my example above then you will have to expand the array to accommodate more strings. Do that by increasing the value of arraySize by some value, say 10.
c Syntax (Toggle Plain Text)
// declare initial 2d array; char **array = 0; // number of strings it will hold int arraySize = 0; // allocate a block of 10 pointers which will store 10 strings arraySize = 10; array = realloc(array, arraySize * sizeof(char *)); // store a string in the 0th array element array[0] = strdup("Hello World");
You will put the last few lines inside the loop that reads the file. Use another integer to count the number of lines that have been read. When that counter is the same as arraySize in my example above then you will have to expand the array to accommodate more strings. Do that by increasing the value of arraySize by some value, say 10.
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.
•
•
Join Date: Apr 2007
Posts: 9
Reputation:
Solved Threads: 0
Hi, I have used it with fixed size array, and the code is below.
But, I want to make it in c++, rather native C, & I want to declare the array as wchar_t cards[10][100]. But, I think fgets only returns char*. so, how can I convert it to wchar_t. or is there any alternate function to fgets, which returns the current line, and the return type as wchar_t
c Syntax (Toggle Plain Text)
int main() { FILE * pFile; char cards[10][100]; int i=0,n=0; pFile = fopen ("c:\\cardnames.txt" , "r"); if (pFile == NULL) perror ("Error opening file"); else { while(fgets (cards[i] , 100 , pFile)) i++; fclose (pFile); } n=i; for(i=0;i<n;i++) printf("%s",cards[i]); return 0; }
Last edited by WaltP; May 2nd, 2007 at 3:17 am. Reason: Please learn to use CODE tags -- read the announcements at the top of the forum
Look into iostreams.
Also, read the announcements at the top of the forum and learn to format your code.
Also, read the announcements at the top of the forum and learn to format your code.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- connect to text file database (Visual Basic 4 / 5 / 6)
- Output in the text file (C)
- Create stats from a text file (Java)
- Store Bluetooth remote address to a text file (C++)
- 10 line text file (Java)
- Read and write to an ASCII Text file (Java)
Other Threads in the C Forum
- Previous Thread: hI.. HoW 2 InStAlL a wxWidgets.. and how to compile a project in wxWidgets
- Next Thread: dealing with object files
| Thread Tools | Search this Thread |
#include * adobe ansi api array asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global grade graphics gtkgcurlcompiling gtkwinlinux hacking highest histogram include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft mqqueue number odf opendocumentformat owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape single socket socketprograming standard string systemcall threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






