Thread: Problem parsing comments and such in text file

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    2

    Question Problem parsing comments and such in text file

    Hello,

    I got program that opens a file for reading with fscanf putting the info into an array of structs, working fine so far.

    This part looks like this:
    Code:
    /* snip */
    
    fscanf(datei, "%s %d", &datensatz[z].fsname, &datensatz[z].grenze);
    
    while(!feof(datei)){
         z++;
         fscanf(datei, "%s %d", &datensatz[z].fsname, &datensatz[z].grenze);
    }
    
    /* snip */

    Such a text file contains a filesystem name and a number representing it's filling threshold like for example:

    /usr 80
    /home 70
    /tmp 90

    I now want to add comments into the textfile, beginning with a '#' and blank lines like for example:

    # infile for tool platzcheck
    # format: fsname threshold

    /usr 80
    /home 70
    /tmp 90

    Any ideas how I can implement this? Any help or just a little hint would be fine.
    Thanks in forward!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) Open a file for writing.
    2) Write your comments and blank lines.
    3) Open the file you wish to modify, for reading.
    4) While not at the end of the input file:
    ... a) Read a line.
    ... b) Write said line to the output file.
    5) Close both files.
    6) Rename or delete the origional file.
    7) Rename the new file to the name of the other.

    ..or...

    1) Open the file:
    ... a) for reading and writing (r+)
    ... b) for reading
    2) Read the entire thing into memory using a linked list or what not.
    3) See below:
    ... a) Rewind the file.
    ... b) Close the file, delete the file, and open for writing with the same name.
    4) Write the whole thing back.

    Naturally some of these options are safer or smarter than others. Have fun.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I now want to add comments into the textfile, beginning with a '#' and blank lines like for example:
    The only function you should ever use for reading a text file is fgets()

    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, datei ) != NULL ) {
      if ( buff[0] == '#' ) {
        continue;  // its a comment
      } else if ( sscanf( buff, "%s %d", datensatz[z].fsname, &datensatz[z].grenze) == 2 ) {
        z++;  // line decoded OK
      } else {
        // bad line
    }
    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.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    2
    Got it, ty

Popular pages Recent additions subscribe to a feed