Thread: How can I parse data from a file

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    5

    How can I parse data from a file

    hi:
    Here are some dataset:
    =================
    dog;This is a dog;11
    cat;no cat here;4
    !EMPTY!;blabla;-1
    blablabla

    Here is the function to read data:
    =======================
    Code:
        while (fscanf(inputFp, "%s;%s;%d", Pattern.string, Text.string,    &expectedIndex) != EOF )  {
             ...........
        }
    The problem is that how can I read "This is a dog" into Text.string.
    Should I use fscanf, or I can use something simpler?

    Appreciate for help.

    @figo2476

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    How about the strtok() function?
    http://www.cppreference.com/stdstring/strtok.html

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    13
    I am not sure how it works with fscanf, because the length of the string which is to be read, is not specified.

    One way is use fgetc(file pointer) and read each character, and when you find a ';', look for first 4 characters. if the first 4 characters string is "This", then read the rest of the characters until you find a ';' again.
    Last edited by spveer; 08-18-2005 at 02:38 AM.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    5

    reply

    I tried this and I found something intresting:

    Here is my code:
    ---------------------
    Code:
    int main(int argc, char *argv[])
    {
    
      FILE *inputFp;
      FILE *outputFp;
    	
      int shift = 0;
      STRING Pattern;
      STRING Text;
      char buffer[BUFFER_LEN];
      char lenArray[100];
      char field[200];
      char *ptr = NULL;
      char *patternTmp, *textTmp, *expect; 
      int len;
      int n;
    
      inputFp = fopen(INPUT, "r");
      if (inputFp == NULL) {
    	printf("Can't open input file: TestCases.in");
    	exit(0);
      }
      
      outputFp = fopen(OUTPUT, "w");
      if (outputFp == NULL) {
    	printf("Can't open output file: TestResults.out");
    	exit(0);
      }
    
      /* Read the input file */
      while (fgets(buffer, BUFFER_LEN, inputFp)) {
    	ptr = strtok(buffer, "\n"); 
    	while (sscanf(ptr, "%199[^;]%*c%n", field, &n) == 1) {
    		ptr += n;
    		printf("field = %s\n", field);
    	}
    
      }
    
      fclose(inputFp);
      fclose(outputFp);
    
    
    My input file
    ----------------
    dog;This is a dog;11
    cat;no cat here;4
    EMPTY;blabla;-1
    EMPTY;EMPTY;1
    dog;This dog;11
    
    
    My output 
    -------------
    field = dog
    field = This is a dog
    field = 11
    field = cat
    field = no cat here
    field = 4
    field = EMPTY
    field = blabla
    field = -1
    field =                       /* I am  not sure this */
    field = EMPTY
    field = EMPTY
    field = 1
    field = dog
    field = This dog
    field = 11
    
    }

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Thanks for finding that. Perhaps this would work better.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       static const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          char line [ BUFSIZ ];
          while ( fgets(line, sizeof line, file) != NULL )
          {
             int n;
             char field [ BUFSIZ / 2 ], *ptr = strtok(line, "\n");
             printf("line  = \"%s\"\n", line);
             while ( sscanf(ptr, "%199[^;]%n", field, &n) == 1 )
             {
                printf("field = %s\n", field);
                ptr += n;
                if ( *ptr != ';' )
                {
                   break;
                }
                ++ptr;
             }
             putchar('\n');
          }
          fclose(file);
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    
    /* file.txt
    dog;This is a dog;11
    cat;no cat here;4
    EMPTY;blabla;-1
    EMPTY;EMPTY;1
    dog;This dog;11
    */
    
    /* my output
    line  = "dog;This is a dog;11"
    field = dog
    field = This is a dog
    field = 11
    
    line  = "cat;no cat here;4"
    field = cat
    field = no cat here
    field = 4
    
    line  = "EMPTY;blabla;-1"
    field = EMPTY
    field = blabla
    field = -1
    
    line  = "EMPTY;EMPTY;1"
    field = EMPTY
    field = EMPTY
    field = 1
    
    line  = "dog;This dog;11"
    field = dog
    field = This dog
    field = 11
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Editing a data file
    By Strait in forum C++ Programming
    Replies: 7
    Last Post: 02-05-2005, 04:21 PM
  5. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM