当前位置: 代码迷 >> C语言 >> 文件读取
  详细解决方案

文件读取

热度:366   发布时间:2006-03-01 13:43:00.0
文件读取
文件读取
您好,我在电脑上保存了一个文本文件in.txt,内容是
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0

我想编写一个程序使它读到数组a[9]中,怎么办?
搜索更多相关的解决方案: 文件  txt  编写  

----------------解决方案--------------------------------------------------------
看书~
----------------解决方案--------------------------------------------------------
2楼说点具体的好不好,用fopen打in.txt,不是最好的办法,但可以。
----------------解决方案--------------------------------------------------------
以下是引用双云在2006-3-1 13:43:00的发言:
文件读取
您好,我在电脑上保存了一个文本文件in.txt,内容是
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0

我想编写一个程序使它读到数组a[9]中,怎么办?

用fopen函数打开文件用fscanf读数据。


----------------解决方案--------------------------------------------------------

也可以用fseek函数


----------------解决方案--------------------------------------------------------
假设你把文件放到了D盘

#include "stdio.h"
main()
{
float a[9],i,*p;
FILE *fp;
p=a;
fp=fopen("d:\\in.txt","rt");
if(fp==NULL)
{
printf("Error!");
getch();
exit(1);
}
fread(p,sizeof(int),9,fp);
for(i=0;i<9;i++)
printf("%d ",*(a+i));
fclose(fp);
getch();
}


----------------解决方案--------------------------------------------------------
#includ <fstream.h>

ofstream xxx("d:\\filename.txt");

while(!xxx.eof())
{
for()
{}
}

就可以了
----------------解决方案--------------------------------------------------------
好想都不行
----------------解决方案--------------------------------------------------------
那不行啊!说出来大家研究研究!
----------------解决方案--------------------------------------------------------
  相关解决方案