parsing file and getting the alternate token count of matching tokens

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shivam001
    New Member
    • Mar 2007
    • 2

    parsing file and getting the alternate token count of matching tokens

    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.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define FNAME "c:\\CProject\\input.txt"
    #define OFNAME "c:\\CProject\\output.txt"
     
    static FILE *fptr;
    static FILE *fp ; 
     
    void pause()
    {
       printf("\nProgram ended, Press ENTER: ");
       fflush(stdin);
       getchar();
    }
     
    void close_the_file()
    {
       close(fptr);
    }
     
    main()
    {
       char buffer[300];
       int count = 0;
     
       atexit(pause);
       fptr = fopen(FNAME, "r"); //open the text file for reading
       if (fptr == NULL)
       {
          perror("Could not open " FNAME);
          exit(1);
       }
       atexit(close_the_file);
       if((fp = fopen(OFNAME, "w"))==NULL) //Read all lines from the file
       {
          printf("Cannot open file.\n");
          exit(1);
       }
     
       char lines[10000];
       int j,k;
       j=0; 
       while (fgets(buffer, sizeof buffer, fptr) != NULL)
       {
    
          // ++count; 
          while(!feof(fptr))
          { // loop through and store the lines into the array 
             fscanf(fptr, "%c", &lines[j]);
             j++;
             // fprintf(fp, "%c", lines[j]);
          }
     
          // ***** to do - read rows and then parse the row string ***** 
     
          // printf("The lines are:\n",&lines[j]);
          printf("Number of lines read: %d\n\n", j);
          for(k=0 ; k<j ; k++)
          { 
             fprintf(fp, "%c", lines[k]);
          } 
       } 
       return 0;
    }
    Last edited by Ganon11; Mar 14 '07, 08:23 PM. Reason: code tags added, indented
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #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 ...

    Comment

    • shivam001
      New Member
      • Mar 2007
      • 2

      #3
      Originally posted by arne
      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

      Comment

      • arne
        Recognized Expert Contributor
        • Oct 2006
        • 315

        #4
        Originally posted by shivam001
        "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?

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          Originally posted by arne
          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.

          Comment

          • lqdeffx
            New Member
            • Mar 2007
            • 39

            #6
            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.

            Comment

            • arne
              Recognized Expert Contributor
              • Oct 2006
              • 315

              #7
              Originally posted by sicarie
              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.

              Comment

              • arne
                Recognized Expert Contributor
                • Oct 2006
                • 315

                #8
                Originally posted by shivam001
                "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.

                Comment

                • sicarie
                  Recognized Expert Specialist
                  • Nov 2006
                  • 4677

                  #9
                  Originally posted by arne
                  '4' is also mentioned for MANGO and ORANGE. Doesn't change the alogrithm, of course.
                  Oh, good catch.

                  Comment

                  Working...