急问,如果读取数据文件到数组中?

来源:百度知道 编辑:UC知道 时间:2024/05/17 01:16:28
vc++: dat后缀的文件,用记事本就能打开,路径 f:\\xt.dat,打开后是如下的一串数字,每行数据前面是空格,后面是一个空格然后回车,我现在想把这些数据读入到数组float a[100]中,我该怎么编写?请给我一个具体的程序,谢谢了,我现在急要!
0.9600469
1.052137
1.153987
1.259709
1.362521
1.461457
1.56191
1.670515
1.789631
1.915926
2.043213
2.166344
2.283283
2.394971
2.504159
2.613817
2.725178
2.836341
2.943142
3.042471
3.135453
3.227471
3.32468
3.429878
3.541087
3.653608
3.763266
3.86799
3.967319
4.061929
4.154603
4.250858
4.356525
4.473231
4.596696
4.720652
4.842548
4.964801
5.091145
5.224274
5.368422
5.531602
5.719733
5.924724
6.119576
6.270328
6.357884
6.390545
6.395433
6.397357
6.405002
6.42775
6.43243
6.430408

----------我已经在vc++中调试过了,保证准确!----------

void GetData(float * a ,int* count)
{
char dat[30];
FILE* fp;
fp=fopen("f:\\xt.dat","r");
if(fp==NULL)
{
MessageBox("can not open file");
return;
}
for(int i=0;!feof(fp);i++)
{
fscanf(fp,"%s",&dat);
a[i]=atof(dat);
}
*count=i;//返回数组元素个数
fclose(fp);

}

这样只要调用了GetData( )函数,
比如在某个地方
float a[100];
int count;

GetData(a,&count)
那么数组a就被装入文件里的数据了,而count带回了数组元素个数