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 397,816 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,622 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:
Views: 1307 | Replies: 1
Reply
Join Date: Mar 2007
Posts: 1
Reputation: shivam_ng is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
shivam_ng shivam_ng is offline Offline
Newbie Poster

File parsing and then parsing the string

  #1  
Mar 13th, 2007
I have the following file as the input

APPLE 0 118 1 110 1 125 1 135 2 110 3 107 3 115 3 126
ORANGE 0 112 1 119 2 109 2 119 3 112 4 109 4 128
MANGO 0 136 1 143 2 143 3 143 4 136
BANANA 0 5 1 12 1 15 2 13 3 6 3 9

I need to read the above file and have the following information in the output file

In APPLE 0 occurs 1 time, 1 occurs 3 times, 2 occurs 1 time, 3 occurs 3 times
In ORANGE 0 occurs 1 time, 1 occurs 1 times, 2 occurs 2 time, 3 occurs 1 times, 4 occurs 2 times
In MANGO 0 occurs 1 time, 1 occurs 1 times, 2 occurs 1 time, 3 occurs 1 times, 4 occurs 1 times
In BANANA 0 occurs 1 time, 1 occurs 2 times, 2 occurs 1 time, 3 occurs 3 times

I started off with reading the input file, putting all the rows into an array. Now I am stuck at reading the row string from the array. Once I get the string, I want to parse the string and get the tokens. This is my idea. Do you have any suggestions to do this.

Following is the code I did to start with.
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define FNAME "c:\\CProject\\input.txt"
  4. #define OFNAME "c:\\CProject\\output.txt"
  5.  
  6. static FILE *fptr;
  7. static FILE *fp ;
  8.  
  9. void pause()
  10. {
  11. printf("\nProgram ended, Press ENTER: ");
  12. fflush(stdin);
  13. getchar();
  14. }
  15.  
  16. void close_the_file()
  17. {
  18. close(fptr);
  19. }
  20.  
  21. main()
  22. {
  23. char buffer[300];
  24. int count = 0;
  25.  
  26. atexit(pause);
  27. fptr = fopen(FNAME, "r"); //open the text file for reading
  28. if (fptr == NULL)
  29. {
  30. perror("Could not open " FNAME);
  31. exit(1);
  32. }
  33. atexit(close_the_file);
  34. if((fp = fopen(OFNAME, "w"))==NULL) //Read all lines from the file
  35. {
  36. printf("Cannot open file.\n");
  37. exit(1);
  38. }
  39.  
  40. char lines[10000];
  41. int j,k;
  42. j=0;
  43. while (fgets(buffer, sizeof buffer, fptr) != NULL)
  44. {
  45. fflush(stdin);
  46. // ++count;
  47. while(!feof(fptr))
  48. { // loop through and store the lines into the array
  49. fscanf(fptr, "%c", &lines[j]);
  50. j++;
  51. // fprintf(fp, "%c", lines[j]);
  52. }
  53.  
  54. // ***** to do - read rows and then parse the row string *****
  55.  
  56. // printf("The lines are:\n",&lines[j]);
  57. printf("Number of lines read: %d\n\n", j);
  58. for(k=0 ; k<j ; k++)
  59. {
  60. fprintf(fp, "%c", lines[k]);
  61. }
  62. }
  63. return 0;
  64. }
Last edited by Ancient Dragon : Mar 13th, 2007 at 12:39 pm. Reason: added code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,643
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 867
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: File parsing and then parsing the string

  #2  
Mar 13th, 2007
The fflush on line 45 is non-standard and may cause undefined behavior. fflush() is for flushing output streams, not input streams.

Is that a C or a C++ program? If it is supposed to be C then rename the file to *.c instead of *.cpp and the compiler will treat it as C program. Then you will have to move lines 40 and 41 to the top of the function.

>>line 60 fprintf(fp, "%c", lines[k]);

Since variable lines is a simple char array why not teat it as one?
fprintf(fp, "%s\n", lines);
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Reply

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

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

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

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