Thread: Parsing parameters from a text file in a random order

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    19

    Parsing parameters from a text file in a random order

    Good afternoon,

    I am trying to write a module to read an unknown, albeit very small (10-15), number of parameters from a text file( such as "Width: 200\n" ). However, I want to be able to specify the parameters in any order and omit some if necessary.

    I figured I should loop through all the lines using fscanf, continuously checking the return value after each iteration and acting appropriately. (i.e if the parameter is read, exit the loop and carry on to the next parameter, otherwise if we hit the EOF marker and the value was not found, assign it a
    default value)

    Code:
    width = -1; // assign a dummy value
    
    while ( (fc = fscanf(filePtr, "Width:%d\n", &width)) != EOF )
    {
       if( fc == 1)
          break; // parameter was read so exit the loop
    }
    
    if( width == -1 )
       width = 300; // parameter not found so default it
    However, this approach doesn't work for some reason.
    Any help would be appreciated.

    Duetti

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    How about trying the old fgets/sscanf combo?
    Code:
    char buffer[BUFSIZ];
    int width = -1;
    
    while ( fgets(buffer, sizeof buffer, filePtr) )
    {
       if ( sscanf(buffer, "Width:%d", &width) == 1 )
       {
          break;
       }
    }
    
    if ( width == -1 )
    {
       width = 300;
    }
    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.*

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    19
    Thanks for the tip Dave. This combo works fine indeed.

    Code:
    while ( fgets(buffer, 1024, in) )
    {
       // the first condition will fail so the second will be discarded automatically
       if ( widthRead == 0 && sscanf(buffer, "Width:%d",  &gContext.width) == 1 )
          widthRead = !widthRead;
       else if ( heightRead == 0 && sscanf(buffer, "Height:%d", &gContext.height) == 1 )
          heightRead = 1;
       else if ( initXYRead == 0 && sscanf(buffer, "InitXY(%d,%d)", &gContext.initX, &gContext.initY) == 2 )
          initXYRead = 1;
       else if ( CColRead == 0 && sscanf(buffer, "CCol:(%f,%f,%f)", &gContext.CCol.r, &gContext.CCol.g, &gContext.CCol.b) == 3 )
          CColRead = 1;
       else if ( i<10 && sscanf(buffer, "RGB(%f,%f,%f)", &gContext.preCol[i].r, &gContext.preCol[i].g, &gContext.preCol[i].b) == 3 )
          i++;}
    }
    I wonder why the other piece of code didn't work...
    This solution does what it was designed to do, however it seems like an overkill to me (even though I can't think of a more elegant solution).

    Duetti
    Last edited by Duetti; 11-20-2003 at 05:17 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I wonder why the other piece of code didn't work...
    Because you blew your chance to try and reparse the data with a different conversion (you used fscanf).

    With fgets(), you have as many goes as it takes with the SAME buffer to decide what it all means
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    19
    >Because you blew your chance to try and reparse the data with a different conversion (you used fscanf).

    What do you mean?

    use fscanf twice? once in the loop continuation condition and the second time inside the body of the loop?

    Duetti

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    fscanf() reads the file, and tries to parse the data.
    The problem is, you've already read the file, so there's no real possibility of backing up and trying again with a different conversion string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    19
    But I used rewind() to go back to the beginning of the file, i just didn't include the function call in the above code fragment.

    The problem with the initial code fragment was that it couldn't locate the string "Width:%d" anywhere in the file, even though it was there.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>even though it was there.
    Using fscanf() means that if the data read doesn't match your requirements, the read stops. If you have a line of text your program isn't expecting at all, you'll find yourself in an endless loop.

    The best way, as shown, is to use fgets() then parse each line in memory.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Nov 2003
    Posts
    19
    Yes, the read stops but the file pointer now points to the next line, right? Will it not keep reading lines until the EOF is reached or the string is found? If you test for the EOF after each iteration, how can you get stuck in an infinite loop?

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Yes, the read stops but the file pointer now points to the next line, right?

    No. Reading by lines is done with fgets. [edit]Or very thorough and careful understanding of how to use fscanf's directives.[/edit]

    >Will it not keep reading lines until the EOF is reached or the string is found?

    No.
    Last edited by Dave_Sinkula; 11-20-2003 at 07:54 PM.
    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. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Reading random line from a text file
    By helloamuro in forum C Programming
    Replies: 24
    Last Post: 05-03-2008, 10:57 PM
  3. Read word from text file (It is an essay)
    By forfor in forum C Programming
    Replies: 7
    Last Post: 05-08-2003, 11:45 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM