【发布时间】:2015-03-12 14:09:33
【问题描述】:
假设我有文件
5f2
3f6
2f1
代码:(printf 应该打印第二个数字(即 2,6 和 1)但它没有
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main (int argc, char * argv[])
{
FILE *ptr;
char str[100];
char * token;
int a, b, i;
int arr[4];
if(argc > 1)
{
ptr = fopen(argv[1],"r");
if(ptr == NULL)
{
exit(1);
}
}
else
{
exit(1);
}
//And I'm looking to parse the numbers between the "f" so..
while(fgets(str,100,ptr) != NULL)
{
token = strstr(str,"f");
if(token != NULL)
{
a = atol(str); // first number
b = atol(token+1); // second number
arr[i] = b; // store each b value (3 of em) into this array
}
i++;
printf("Values are %d\n",arr[i]); //should print 2,6 and 1
}
}
我试图将 printf 移到循环之外,但这似乎打印出更奇怪的结果,我之前看过有关将整数从文件存储到数组中的帖子,但是由于这涉及使用 strstr,所以我我不确定程序是否相同。
【问题讨论】:
-
i++; printf("Values are %d\n",arr[i]);-->i=0;...printf("Values are %d\n",arr[i++]);和file-->ptr
标签: c parsing pointers printing