我需要使用 fscanf()
从多行中读取包含整数的文件。
第一个整数在每一行都没有用;其余的我需要阅读。
我是这样做的
do {
fscanf(fs1[0],"%d%c",&x,&y);
//y=fgetc(fs1[0]);
if(y!='\n') {
printf("%d ",x);
}
} while(!feof(fs1[0]));
但徒劳无功。例如,
101 8 5
102 10
103 9 3 5 6 2
104 2 6 3 8 7 5 4 9
105 8 7 2 9 10 3
106 10 6 5 4 2 3 9 8
107 3 8 10 4 2
我们要读书
8 5
10
9 3 5 6 2
2 6 3 8 7 5 4 9
8 7 2 9 10 3
10 6 5 4 2 3 9 8
3 8 10 4 2
最佳答案
在您读取字符串中的文件后,(fgets) 您可以使用 (strtok) 来拆分字符串,然后使用 (sscanf) 读取整数。
strtok :
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL) {
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
sscanf :
int number = 0;
if(sscanf(pch, "%d", &number) ;
关于c - 如何使用 fscanf() 读取包含整数的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17379684/