hurricane Posted August 2, 2004 Share Posted August 2, 2004 (edited) 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 August 2, 2004 by hurricane Link to comment Share on other sites More sharing options...
0 bithub Posted August 2, 2004 Share Posted August 2, 2004 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 More sharing options...
0 bithub Posted August 2, 2004 Share Posted August 2, 2004 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 More sharing options...
0 Andareed Posted August 2, 2004 Share Posted August 2, 2004 Check strchr as well. Link to comment Share on other sites More sharing options...
0 hurricane Posted August 2, 2004 Author Share Posted August 2, 2004 thanks for your help guys :) Link to comment Share on other sites More sharing options...
Question
hurricane
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 hurricaneLink to comment
Share on other sites
4 answers to this question
Recommended Posts