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

如题所述

#include <stdio.h>
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;
}
温馨提示:答案为网友推荐,仅供参考