C语言怎么从TXT文件中读入数据?

2024-12-27 12:02:52
推荐回答(1个)
回答1:

#include
int main()
{
FILE *fp=NULL;
int a[160];
int i=0;
fp=fopen("data.txt","r");
if ( !fp )
{
printf("open file error\n");
return -1;
}
while( !feof(fp) )
{
if ( fscanf( fp , "%d" ,&a[i] ) !=1 )
break ;
i++;
fgetc(fp) ;//过滤掉分隔符
}
fclose(fp);
//以下倒序输出数据
printf("i=%d\n" , i );
while( --i >= 0 )
{
printf("%d," , a[i] );
if ( i %10 == 0 )
printf("\n") ;
}
return 0;
}