• 0

strtok()


Question

i have a string that is read in from a file that is in the format of

name : variable

at the moment i am using strtok to split it up, but i was wondering if there is any alternatives.

also, when using strtok, is it possible to detect whether lines dont have a ":" in them? i have tried using

       char *b = strtok(temp_string,":");
	if (b == NULL)
	{
     fprintf(out_file_pointer,"%s",temp_string);
	}

but that dosnt seem to work, any ideas on where i am going wrong?

Edited by hurricane
Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

To find out if a string has a ':' is easy.

char buff[] = "some random data you want to check for a colon in";
char *pos = buff;
bool found = false;

while(*pos)
{
  if(*pos == ':')
  {
    found = true;
    break;
  }
  pos++;
}

Link to comment
Share on other sites

  • 0

Oh and another way of doing it (less efficient than my first example).

char buff[] = "some long string";
if(!strstr(buff,":"))
  printf("There is no colon in that string");

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.