c语言中读取txt中的数据给数组

来源:百度知道 编辑:UC知道 时间:2024/06/24 15:07:04
写了如下程序
# include <stdio.h>
# include <math.h>
void main()
{
int i;
double A[7];
{
FILE *fp;
if((fp=fopen("C:\Documents and Settings\Administrator\桌面\input.txt","r"))== NULL)
{
printf("cannot open this file\n");
exit(0);
}
for(i=0;i<7;i++) fscanf(fp,"%f",&A[i]);
fclose(fp);
}
}
想把文件input中的数据依次赋给A[i],但是运行发现不行,A[i]的值始终还是等于0
intput中数据如下
0.48
0.95
2.46
0.95
2.5
0.05
0
3个都试了,不行啊!
# include <stdio.h>
# include <math.h>
void main()
{
int i;
double A[7];
{
FILE *fp;
if((fp=fopen("C:\Documents and Settings\Administrator\桌面\input.txt","r"))== NULL)
{
printf("cannot open this file\n");
exit(0);
}
for(i=0;i<7;i++) fscanf(fp,"%lf",A[i]);
printf(&

fscanf(fp,"%f",&A[i]);
改为:
fscanf(fp,"%lf",&A[i]);

printf("%f",A[0]);
改为
printf("%lf",A[0]);
****************************************
一个测试通过的程序:

//---------------------------------------------------------------------------

# include <stdio.h>
# include <math.h>
void main()
{
int i;
double A[7];
{
FILE *fp;
if((fp=fopen("C:\\Documents and Settings\\Administrator\\桌面\\input.txt","r"))== NULL)/*特别注意这里,一定要用双斜杠*/
{
printf("cannot open this file\n");
exit(0);
}
for(i=0;i<7;i++) fscanf(fp,"%lf",&A[i]);/*注意这里*/
printf("%lf",A[0]);/*注意这里*/
fclose(fp);
}
}
//---------------------------------------------------------------------------

***********************************************