CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2005
    Posts
    8

    string parsing still...

    Hey guys,

    I have a config file which it's first few lines are # commented and I want to skip them and start "anazlying" or basically doing string parsing stuff on the rest of the file..

    so I've opened the file and i did something like this:

    fp is the filepointer, str is the string im getting each line with.

    code
    ...
    ...
    while (getline(fp, str)) {
    if ( !str.find_first_of("#") ); {
    cout << str << endl;
    }
    }

    i basically want to do my parsing inside the if statement on everything but the commented lines, so i figured id look for them, and when i dont match them then ill be working on the non-commented lines.
    but it doesnt work that way when i compile it, i just see the entire file printed out...

    any ideas for a simple way of doing this?

    thanks.

  2. #2
    Join Date
    Dec 2004
    Location
    Marlyand, USA
    Posts
    66

    Re: string parsing still...

    If it is the first character this will work:

    if (str[0] != '#')...

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: string parsing still...

    You have a bug in your code: Remove the semicolon after the if statement otherwise the block with the out won't belong the if statement:

    Code:
    while (getline(fp, str)) {
    if ( !str.find_first_of("#") )/* no ; here */ {
    cout << str << endl;
    }
    }
    After reading a line from the config file I would remove any space that is in front of the real line. This will also make the cut out of the comment lines easier. Imagine such a line:

    Code:
                 seta motd "Hello"
      seta sv_strictAuth "0"
    Thus I would make it this way:

    Code:
    while (getline(fp, str)) 
    {
      while ( isspace(str[0]) )
      { // Remove spaces
         str.erase(0, 1);
      }
      if ( str[0] != '#' ) 
      {
        cout << str << endl;
      }
    }
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  4. #4
    Join Date
    Feb 2003
    Posts
    377

    Re: string parsing still...

    Code:
    while (getline(fp, str)) {
      if ( !str.find_first_of("#") ); {
        cout << str << endl;
      }
    }
    1. The semicolon in red means that your if statement has no effect
    2. find_first_of returns string::npos (basically -1) if the string is not found and the index if it is found. Since this is not a boolean test, it is confusing to use the boolean operator! If you are trying to check to see if the return value is zero, it would be better to use:
      Code:
      if ( str.find_first_of("#") == 0 )
    3. If you are just checking to see whether the first character is #, it would be better to just check the first character, instead of wasting time on the entire string:
      Code:
      if ( !str.empty() && str[0] == '#' )


    [Edit] - Beaten, but make sure you check for an empty string before looking at str[0].

  5. #5
    Join Date
    Jul 2005
    Posts
    8

    Re: string parsing still...

    thanks you guys,
    i learned alot from this post (like always ).

    the semicolon there was a typo mistake since i wrote that
    piece of code on the fly, i was probably not paying attention, but thanks

    ill check it when i get home.
    lir.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured