944,025 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5919
  • C RSS
Apr 26th, 2007
0

Text file parser in C

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
satya.vijai is offline Offline
9 posts
since Apr 2007
Apr 26th, 2007
0

Re: Text file parser in C

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

Re: Text file parser in C

I have the following sample.
  1.  
  2. int main()
  3. {
  4. FILE * pFile;
  5. char string [100];
  6.  
  7. // char filename = "c:\\cards.txt";
  8. pFile = fopen ("c:\\cards.txt" , "r");
  9. if (pFile == NULL) perror ("Error opening file");
  10. else {
  11. while(fgets (string , 100 , pFile))
  12. printf("%s",string);
  13. fclose (pFile);
  14. }
  15. return 0;
  16. }
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
Last edited by Ancient Dragon; Apr 26th, 2007 at 9:51 am. Reason: add code tags removed color tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
satya.vijai is offline Offline
9 posts
since Apr 2007
Apr 26th, 2007
0

Re: Text file parser in C

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 27th, 2007
0

Re: Text file parser in C

I did it in the following way.
  1. int main()
  2. {
  3. FILE * pFile;
  4. char string [10][100];
  5. int i=0,n=0;
  6.  
  7.  
  8. pFile = fopen ("c:\\cards.txt" , "r");
  9. if (pFile == NULL) perror ("Error opening file");
  10. else {
  11. while(fgets (string[i] , 100 , pFile))
  12. {
  13. printf("%s",string[i]);
  14. i++;
  15. }
  16.  
  17. fclose (pFile);
  18. }
  19. i=n;
  20. for(i=0;i<n;i++)
  21. printf("%s",string[i]);
  22. return 0;
  23. }
But instead of setting up a fixed length array, pls suggest me the steps to create a dynamic array using malloc(), or realloc().
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
satya.vijai is offline Offline
9 posts
since Apr 2007
Apr 27th, 2007
0

Re: Text file parser in C

If you know how to use pointers then you can do this:
  1. // declare initial 2d array;
  2. char **array = 0;
  3. // number of strings it will hold
  4. int arraySize = 0;
  5.  
  6. // allocate a block of 10 pointers which will store 10 strings
  7. arraySize = 10;
  8. array = realloc(array, arraySize * sizeof(char *));
  9.  
  10. // store a string in the 0th array element
  11. 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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
May 2nd, 2007
0

Re: Text file parser in C

Hi, I have used it with fixed size array, and the code is below.
  1. int main()
  2. {
  3. FILE * pFile;
  4. char cards[10][100];
  5. int i=0,n=0;
  6. pFile = fopen ("c:\\cardnames.txt" , "r");
  7. if (pFile == NULL) perror ("Error opening file");
  8. else
  9. {
  10. while(fgets (cards[i] , 100 , pFile))
  11. i++;
  12. fclose (pFile);
  13. }
  14. n=i;
  15. for(i=0;i<n;i++)
  16. printf("%s",cards[i]);
  17. return 0;
  18. }
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
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
satya.vijai is offline Offline
9 posts
since Apr 2007
May 2nd, 2007
0

Re: Text file parser in C

Look into iostreams.

Also, read the announcements at the top of the forum and learn to format your code.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006

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: hI.. HoW 2 InStAlL a wxWidgets.. and how to compile a project in wxWidgets
Next Thread in C Forum Timeline: dealing with object files





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


Follow us on Twitter


© 2011 DaniWeb® LLC