如何从C中的文件中获取单独创建的整数
在C语言中,如何从像“1,2,3,5,6”这样的文件(到一个数组或一个接一个)中获取分隔的int,而不获取像“”或“,”这样的垃圾(如何从C中的文件中获取单独创建的整数,c,file,int,scanf,C,File,Int,Scanf,在C语言中,如何从像“1,2,3,5,6”这样的文件(到一个数组或一个接一个)中获取分隔的int,而不获取像“”或“,”这样的垃圾(,,是一种可能的情况) 我考虑过strtok,但它只处理字符串,我不知道文件的长度,所以可能fgets不是解决方案。。 我试过这个: fp=fopen("temp.txt","r"); if(fp==NULL) { fprintf(stderr,"%s","Error"); exit(0); } whil
,,
是一种可能的情况)
我考虑过strtok,但它只处理字符串,我不知道文件的长度,所以可能fgets不是解决方案。。
我试过这个:
fp=fopen("temp.txt","r");
if(fp==NULL)
{
fprintf(stderr,"%s","Error");
exit(0);
}
while(fscanf(fp,"%d",&num)!=EOF)
{
printf("first num is %d",&num);
}
但我认为这将是一个问题,因为未知的文件大小和垃圾问题。
你觉得怎么样
谢谢 使用scanf()
的返回值
int chk;
do {
chk = fscanf(fp, "%d", &num);
switch (chk) {
default: /* EOF */;
break;
case 0: fgetc(fp); /* ignore 1 character and retry */
break;
case 1: printf("num is %d\n", num);
break;
}
} while (chk >= 0);
使用scanf()
的返回值
int chk;
do {
chk = fscanf(fp, "%d", &num);
switch (chk) {
default: /* EOF */;
break;
case 0: fgetc(fp); /* ignore 1 character and retry */
break;
case 1: printf("num is %d\n", num);
break;
}
} while (chk >= 0);
下面的程序适用于文件的任何格式,可以提取文件中包含的任何整数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
FILE* f=fopen("file","rb");
/* open the file */
char *str=malloc(sizeof(char)*100);
/* str will store every line of the file */
if (f!=NULL)
{
printf("All the numbers found in the file !\n");
while (fgets(str,100,f)!=NULL)
{
int i=0,n=0;
/* the n will contain each number of the f ile */
for (i=0;i<strlen(str);i++)
{
int test=0;
/* test will tell us if a number was found or not */
while (isdigit(str[i]) && i<strlen(str))
{
test=1;
n=n*10+str[i]-'0';
i++;
}
if(test!=0)
printf("%d\n",n);
/* print the number if it is found */
}
}
fclose(f);
}
free(str);
//free the space allocated once we finished
return 0;
}
它将产生
All the numbers found in the file !
0
12
1
245
希望有帮助 下面的程序适用于文件的任何格式,可以提取文件中包含的任何整数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
FILE* f=fopen("file","rb");
/* open the file */
char *str=malloc(sizeof(char)*100);
/* str will store every line of the file */
if (f!=NULL)
{
printf("All the numbers found in the file !\n");
while (fgets(str,100,f)!=NULL)
{
int i=0,n=0;
/* the n will contain each number of the f ile */
for (i=0;i<strlen(str);i++)
{
int test=0;
/* test will tell us if a number was found or not */
while (isdigit(str[i]) && i<strlen(str))
{
test=1;
n=n*10+str[i]-'0';
i++;
}
if(test!=0)
printf("%d\n",n);
/* print the number if it is found */
}
}
fclose(f);
}
free(str);
//free the space allocated once we finished
return 0;
}
它将产生
All the numbers found in the file !
0
12
1
245
希望有帮助 您的文件的结构是什么?你能发布一个示例文件吗?只有数字、空格和“,”。例如“12213 1212312、、32、、12”。函数fscanf返回成功输入/转换操作数的计数(0或更大),而不是EOF。在本例中,成功时返回1,不成功时返回0,文件末尾将返回什么fscanf?文件的结构是什么?你能发布一个示例文件吗?只有数字、空格和“,”。例如“12213 1212312、、32、、12”。函数fscanf返回成功输入/转换操作数的计数(0或更大),而不是EOF。在本例中,如果成功,它将返回1,如果不成功,它将返回0。文件末尾的fscanf将返回什么?'fgets'用于跳过垃圾?
fgetc()
(带小写C)读取一个字符,该字符不一定是整数的一部分,否则它将被scanf()捕获
fgetc**yeah听起来不错……我将在文件末尾得到什么?在文件末尾,循环终止chk在EOF上得到一个NULL?fgets用于跳过垃圾?fgetc()
(带小写C)读取一个字符,该字符必须不是整数的一部分,否则它将被scanf()捕获
fgetc**yeah听起来不错……我将在文件末尾得到什么?在文件末尾,循环终止chk在EOF上得到一个NULL?sizeof(str)
是当前实现中指针的大小。我想你的意思是100
与fgets()通话。是的,谢谢@pgm:)可能是因为我以前使用阵列!再次感谢sizeof(str)
是当前实现中指针的大小。我想你的意思是100
与fgets()通话。是的,谢谢@pgm:)可能是因为我以前使用阵列!再次感谢!