TC问题求解

来源:百度知道 编辑:UC知道 时间:2024/06/24 06:56:25
现有一个TXT文件如下:
The number of the point is 1000
Number T Y
1 0.000000 0.500000
2 0.000500 0.125519
3 0.001000 0.250172
4 0.001500 0.373099
5 0.002000 0.493458
6 0.002500 0.610425
7 0.003000 0.723208
要把T和Y的数据读入到程序中的T[i]Y[i]中使用如何编

先把文件打开,用函数fgets和sscanf把数据取出来

float atof(char *c)
{
float f;
sscanf(c,"%f",&f);
return f;
}

main(){
FILE *fp;
float T[10],Y[10];
char str[80],ch,s1[20],s2[20],**p;
int i,j;
fp=fopen("filename","rb");
printf("here ok 1");
getch();
i=0;
while(!feof(fp)){
fgets(str,80,fp);
sscanf(str,"%c%s%s",&ch,s1,s2);
T[i]=atof(s1);
Y[i]=atof(s2);
i++;
}
printf("here ok 2");//调试用
getch();
for(j=0;j<i;j++){
printf("\n%f %f",T[j],Y[j]);

}
getch();
}

/*****************
以下程序在vc6.0上测试通过;
文件名为1.txt;将T[i]Y[i]打印在屏幕上;
NUM为文件行数;
*************************/
#include "stdio.h"
#include "stdlib.h"
#define NUM 1000
FILE* fd;
main()
{
int i;
int Number[NUM];
float T[NUM],Y[NUM