您好我使用strtok分割我使用fgets从文件读入的一行。我很想知道如何使用if语句检查该行的每个分区。
文件中的一行就像是
1 FirstName-LastName
char *stuff;
while(fgets(buffer, sizeof(buffer), myfile)){
stuff = strtok(buffer, " ");// this should give me 1
if( ...... ) // what would be the correct way of saying if stuff == "1"
答案 0 :(得分:0)
==
实际上只对比较整数值有用。使用strcmp()
或strstr()
之类的字符串进行比较。
这样的事情。 (请参阅嵌入的评论以获得一些解释......)
char *stuff;
char keep[260];// or some reasonable size to do your comparison.
//its best not to use your token to do anything except store the token :)
while(fgets(buffer, sizeof(buffer), myfile))
{
stuff = strtok(buffer, " ");// this should give me 1
while(stuff) // as long as stuff not NULL
{
strcpy(keep, stuff);//
if(strcmp(keep, "1") == 0) //strtok returns only (char *) and output of strcmp is 0 for equal
{
//do something here!
}
//get next token
stuff = strtok(NULL, " ");
}
}