【发布时间】:2015-03-14 16:28:07
【问题描述】:
假设我有一个文件:
f56,5 d23,4
我正在获取 'f' 和逗号之后的值(与 d 相同),所以我这样做: (在使用 fgets 读取文件时)
while (fgets(buf,100,file) != NULL)
{
temp = strstr(buf,"f"); //where temp is a (char * )
if(temp != NULL)
{
//An int defined previously
x = atol(temp+1); //get the value 56
temp = strstr(buf,","); //get the value 5
y = atol(temp+1); //get the value 5
}
temp = strstr(buf,"d");
if(temp != NULL)
{
a = atol(temp+1); //get the value 24
temp = strstr(buf,","); //get the value 4?
b = atol(temp+1); //get the value 4?
}
}
这种工作,但是,a 和 b 的值不正确,a 有时是正确的,但是 b 始终是 y 的值(以前的逗号值)。我不太确定如何在这里继续,我尝试使用另一个指针在代码中使用strstr,但这似乎不起作用,任何帮助将不胜感激。
【问题讨论】: