By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,619 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

parsing file and getting the alternate token count of matching tokens

P: 2
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.

Expand|Select|Wrap|Line Numbers
  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.  
  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. }
Mar 14 '07 #1
Share this Question
Share on Google+
8 Replies


arne
Expert 100+
P: 315
Is that an assignment you're forced to do in C? If not, you should really consider to use a more appropriate language for this task than C. Perl may be a candidate ...
Mar 14 '07 #2

P: 2
Is that an assignment you're forced to do in C? If not, you should really consider to use a more appropriate language for this task than C. Perl may be a candidate ...
"C" is the requirement
Mar 14 '07 #3

arne
Expert 100+
P: 315
"C" is the requirement
Ok.

I'm not sure if I have fully understood the task. Each line consists of a string and a set of numbers. The numbers are separated by blanks.

The task is then to count the occurences of individual single digit numbers?
Mar 14 '07 #4

sicarie
Expert Mod 2.5K+
P: 4,677
Ok.

I'm not sure if I have fully understood the task. Each line consists of a string and a set of numbers. The numbers are separated by blanks.

The task is then to count the occurences of individual single digit numbers?
I think it's just the occurrences of '1', '2', or '3' without any other numbers around them. In which case, you could use an array of chars, whenever you found one of those, compare the character before it and after it to ' ' or however you want.
Mar 14 '07 #5

P: 39
Why store the lines read from the file and then loop through the array? You could very well just grab each line, parse it and store the data needed and move to the next line of the file.
Mar 14 '07 #6

arne
Expert 100+
P: 315
I think it's just the occurrences of '1', '2', or '3' without any other numbers around them. In which case, you could use an array of chars, whenever you found one of those, compare the character before it and after it to ' ' or however you want.
'4' is also mentioned for MANGO and ORANGE. Doesn't change the alogrithm, of course.

@shivam001: parse a line by searching for the next blank and remember the position of the last. If the difference is 2 you have found a single digit. Define an array to store the occurences of the individual digits. You can use the digits themselves as the index.
Mar 14 '07 #7

arne
Expert 100+
P: 315
"C" is the requirement
Anyway, if you ever come across something like this in real life, remember that there may be more suitable languages for such tasks.
Mar 14 '07 #8

sicarie
Expert Mod 2.5K+
P: 4,677
'4' is also mentioned for MANGO and ORANGE. Doesn't change the alogrithm, of course.
Oh, good catch.
Mar 14 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.