|
-
July 14th, 2005, 11:25 AM
#1
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.
-
July 14th, 2005, 11:40 AM
#2
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
-
July 14th, 2005, 11:42 AM
#3
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;
}
}
-
July 14th, 2005, 11:43 AM
#4
Re: string parsing still...
Code:
while (getline(fp, str)) {
if ( !str.find_first_of("#") ); {
cout << str << endl;
}
}
- The semicolon in red means that your if statement has no effect
- 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 )
- 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].
-
July 14th, 2005, 11:56 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|