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";
       }
}

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.
}

Edited 4 Years Ago by Ancient Dragon

This article has been dead for over six months. Start a new discussion instead.