请问如何用c语言从txt文件中读取数据?

请问如何用c语言从txt文件中读取数据?
就是用空格分开的数据,例如:12 23 34 56 怎么把它们原样读取出来啊?
给个c代码,谢谢了!
能不能留下个qq号联系一下啊

//其中的in.txt就是你要读取数据的文件,当然把它和程序放在同一目录
-------------------------------------

#include <stdio.h>
int main()
{
int data;
FILE *fp=fopen("in.txt","r");
if(!fp)
{
printf("can't open file\n");
return -1;
}
while(!feof(fp))
{
fscanf(fp,"%d",&data);
printf("%4d",data);
}
printf("\n");
fclose(fp);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-12-23
#include<stdio.h>
main()
{
int i=0,j=0;
int a[100];
FILE *fp;
if((fp=fopen("1.txt","rt"))==NULL)
{
printf("error!\n");
getch();
exit(1);
}
while(!feof(fp))
{fscanf(fp,"%d",&a[i]);i++;}
for(j=0;j<i;j++)
printf("%d",a[j]);
fclose(fp);
}
回答者: hwuaxj - 千总 四级 12-23 12:35
//其中的in.txt就是你要读取数据的文件,当然把它和程序放在同一目录
-------------------------------------

#include <stdio.h>
int main()
{
int data;
FILE *fp=fopen("in.txt","r");
if(!fp)
{
printf("can't open file\n");
return -1;
}
while(!feof(fp))
{
fscanf(fp,"%d",&data);
printf("%4d",data);
}
printf("\n");
fclose(fp);
return 0
第2个回答  2008-12-23
#include<stdio.h>
main()
{
int i=0,j=0;
int a[100];
FILE *fp;
if((fp=fopen("1.txt","rt"))==NULL)
{
printf("error!\n");
getch();
exit(1);
}
while(!feof(fp))
{fscanf(fp,"%d",&a[i]);i++;}
for(j=0;j<i;j++)
printf("%d",a[j]);
fclose(fp);
}
相似回答