User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 457,732 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,745 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 393 | Replies: 6
Reply
Join Date: Oct 2008
Posts: 5
Reputation: sanushks is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sanushks sanushks is offline Offline
Newbie Poster

Parsing Log file and writing it into array/file

  #1  
Oct 10th, 2008
I'm a C novice, require help on parsing text files and storing in an array or another text file:

Below is the data which are partly pipe delimited values and part of it are name-value pairs,
for instance the "cma{PL}Ind{1}man{6}gal{0}cif{}" mentioned below
cma is the field name and PL is its value, i need to extract only the values within the braces along with other pipe delimited values. Please suggest.

The data looks like this:

1|2|3|?|feg|jfh||||"cma{PL}Ind{1}man{6}gal{0}cif{}"|
111|233|333|?|media|jack|||||"cma{PPS}Ind{1}man{3}gal{0}cif{234}ink{0}"|

Need to store the values in a array.


Please help!!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2008
Posts: 231
Reputation: Freaky_Chris is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 27
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Posting Whiz in Training

Re: Parsing Log file and writing it into array/file

  #2  
Oct 10th, 2008
strtok() would help. read some online documentation on it.

It splits a string based on a delimiantor
such as a pipe, this would allow you to retrieve all the different ssctions between the pipes. Then you could further process the {} out of the data.

Chris
Knowledge is power -- But experience is everything
Reply With Quote  
Join Date: Apr 2004
Posts: 3,756
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Parsing Log file and writing it into array/file

  #3  
Oct 10th, 2008
One of these, perhaps?
Reply With Quote  
Join Date: Oct 2008
Posts: 5
Reputation: sanushks is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sanushks sanushks is offline Offline
Newbie Poster

Re: Parsing Log file and writing it into array/file

  #4  
Oct 11th, 2008
That program gave a good idea.. But suppose i need to write the contents of the text file into another text file after the file parsing, do i need to store it in a array or can do itthe read and write operation directly.

For ex:
Input file
1|2|3|?|feg|jfh||||"cma{PL}Ind{1}man{6}gal{0}cif{}"|

Output file:
1|2|3|?|feg|jfh||||PL|1|6|0||

that is as i mentioned earlier i need the values withhin the double quotes parsed only with values

Could you suggest a code for this please?
Reply With Quote  
Join Date: Sep 2008
Posts: 272
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Rep Power: 2
Solved Threads: 41
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Parsing Log file and writing it into array/file

  #5  
Oct 11th, 2008
You can read and write almost directly
Reply With Quote  
Join Date: Jul 2008
Location: Bangalore, India
Posts: 104
Reputation: ahamed101 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 13
ahamed101's Avatar
ahamed101 ahamed101 is offline Offline
Junior Poster

Re: Parsing Log file and writing it into array/file

  #6  
Oct 11th, 2008
You can read and write simultaneously like Sci@phy suggested...

1. Open file 1 in read mode
2. Open file 2 in append mode
3. read from file 1
4. do the processing
5. write to file 2
6. continue from step 3 till end of file of file 1
7. close file 2
8. close file 1
Last edited by ahamed101 : Oct 11th, 2008 at 2:18 pm. Reason: Corrected Post
regards,
Ahamed.
Reply With Quote  
Join Date: Oct 2008
Posts: 5
Reputation: sanushks is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sanushks sanushks is offline Offline
Newbie Poster

Re: Parsing Log file and writing it into array/file

  #7  
Oct 13th, 2008
Thanks for the suggestion, I'm having trouble while writing to another file.
Suppose i have 2 lines in the input file,the output file has one line. I know its the newline character problem,But not sure how to solve it. One more problem is since i have 2 delimiters( | and {}) within the same line, how do i actually append it to the same line after the processing.

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main(void)
  5. {
  6. static const char filename[] = "file.txt"; /* the name of a file to open */
  7. FILE * pFile;
  8. FILE *file = fopen(filename, "r"); /* try to open the file */
  9. pFile = fopen ("myfile.txt","a");
  10. if ( file != NULL )
  11. {
  12. char line[BUFSIZ]; /* space to read a line into */
  13.  
  14. char data[1][32];
  15. while ( fgets(line, sizeof line, file) != NULL ) /* read each line */
  16. {
  17. size_t i = 0, size;
  18. char *token = line;
  19. fputs(line, stdout);
  20. for ( ;; )
  21. {
  22. size_t len = strcspn(token, ";\n"); /* search for delimiters */
  23. sprintf(data[i], "%.*s", (int)len, token);
  24. fprintf (pFile, "%s|",data[0]);
  25.  
  26. token += len; /* advance pointer by the length of the found text */
  27. if ( *token == '\0' )
  28. {
  29. break; /* advanced to the terminating null character */
  30. }
  31. ++token; /* skip the delimiter */
  32. }
  33.  
  34. }
  35.  
  36. fclose(file);
  37. fclose(pFile);
  38. }
  39. else
  40. {
  41. perror(filename); /* why didn't the file open? */
  42. }
  43. return 0;
  44. }
Last edited by cscgal : Oct 13th, 2008 at 12:25 pm. Reason: Added code tags
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 7:33 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC