Text file parser in C

Reply

Join Date: Apr 2007
Posts: 9
Reputation: satya.vijai is an unknown quantity at this point 
Solved Threads: 0
satya.vijai satya.vijai is offline Offline
Newbie Poster

Text file parser in C

 
0
  #1
Apr 26th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Text file parser in C

 
0
  #2
Apr 26th, 2007
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 9
Reputation: satya.vijai is an unknown quantity at this point 
Solved Threads: 0
satya.vijai satya.vijai is offline Offline
Newbie Poster

Re: Text file parser in C

 
0
  #3
Apr 26th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Text file parser in C

 
0
  #4
Apr 26th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 9
Reputation: satya.vijai is an unknown quantity at this point 
Solved Threads: 0
satya.vijai satya.vijai is offline Offline
Newbie Poster

Re: Text file parser in C

 
0
  #5
Apr 27th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Text file parser in C

 
0
  #6
Apr 27th, 2007
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 9
Reputation: satya.vijai is an unknown quantity at this point 
Solved Threads: 0
satya.vijai satya.vijai is offline Offline
Newbie Poster

Re: Text file parser in C

 
0
  #7
May 2nd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Text file parser in C

 
0
  #8
May 2nd, 2007
Look into iostreams.

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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC