We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,275 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

strcmp question

lets say i there is a file with 2 lines in it. and we going to read from it.

only thing we dont how how many spaces there are. for ex " -hello ", "    -hello", "-hello", "  -hello    "



 file.txt
-----------
     -hello 
-hello 29 
-----------

i want to skip line that has only "-hello" in it, NOT "-hello 29"

the way i was think was

char line[20];

//read one line at a time

while((fget(line, 20), finp1)!= NULL)
{
    if(strcmp(line, "-hello") != 0)
       {
           //HERE LINE SHOULD HAVE THE VALUE "-hello 29";
       }
}
2
Contributors
1
Reply
1 Hour
Discussion Span
1 Year Ago
Last Updated
2
Views
hwoarang69
Posting Pro
569 posts since Feb 2012
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 7

strcmp() won't work for you because it expects an exact match.

you can call strstr() to see if the line contains "-hello". If it does, then check for anything following to see if something other than spaces. You may need to do additional checks if there is a chance that the line will contains other stuff, for example "-HelloWorld" because strstr() will return valid pointer for that also.

char* ptr = strstr(line, "-hello");
if( ptr != NULL)
{
   // now check remainder of string to see if 
   // there is anything but spaces following the
   // text -Hello.
}
Ancient Dragon
Achieved Level 70
Team Colleague
32,144 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0605 seconds using 2.65MB