将txt文件中的数据读入数组中
下面是源码
#include <stdio.h>
#include <stdlib.h>
struct gene
{
int code;
float Medium1;
float ToxA1;
float ToxB1;
float ToxAB1;
float ToxoidAB1;
float Medium2;
float ToxA2;
float ToxB2;
float ToxAB2;
float ToxoidAB2;
float Medium3;
float ToxA3;
float ToxB3;
float ToxAB3;
float ToxoidAB3;
struct gene *next;
};
struct gene*creat(struct gene *h,FILE*fp);
struct gene*insert(struct gene *h,FILE*fp);
void spnt(struct gene *h);
int main()
{
struct gene *h=NULL;
FILE*fp;
fp=fopen("data1.txt","r");
creat(h,fp);
insert(h,fp);
return 0;
}
void spnt(struct gene *h)
{
if(h==NULL)
{
printf("error!");
return;
}
else
{
while(h!=NULL)
{
printf("%d %f %f %f %f %f ",h->code,h->Medium1,h->ToxA1,h->ToxB1,h->ToxAB1,h->ToxoidAB1);
printf("%f %f %f %f %f ",h->Medium2,h->ToxA2,h->ToxB2,h->ToxAB2,h->ToxoidAB2);
printf("%f %f %f %f %f",h->Medium3,h->ToxA3,h->ToxB3,h->ToxAB3,h->ToxoidAB3);
h=h->next;
}
}
}
struct gene*creat(struct gene*h,FILE*fp)
{
h=(struct gene*)malloc(sizeof(struct gene));
fscanf(fp,"%d %f %f %f %f %f",&h->code,&h->Medium1,&h->ToxA1,&h->ToxB1,&h->ToxAB1,&h->ToxoidAB1);
fscanf(fp,"%f %f %f %f %f",&h->Medium2,&h->ToxA2,&h->ToxB2,&h->ToxAB2,&h->ToxoidAB2);
fscanf(fp,"%f %f %f %f %f\n",&h->Medium3,&h->ToxA3,&h->ToxB3,&h->ToxAB3,&h->ToxoidAB3);
h->next=NULL;
spnt(h);
printf("\n%d",ftell(fp));
return h;
}
struct gene*insert(struct gene *h,FILE*fp)
{
struct gene *node=NULL,*end=NULL;
end=h;
while(!feof(fp))
{
node=(struct gene*)malloc(sizeof(struct gene));
fscanf(fp,"%d %f %f %f %f %f",&node->code,&node->Medium1,&node->ToxA1,&node->ToxB1,&node->ToxAB1,&node->ToxoidAB1);
fscanf(fp,"%f %f %f %f %f",&node->Medium2,&node->ToxA2,&node->ToxB2,&node->ToxAB2,&node->ToxoidAB2);
fscanf(fp,"%f %f %f %f %f\n",&node->Medium3,&node->ToxA3,&node->ToxB3,&node->ToxAB3,&node->ToxoidAB3);
end->next=node;
end=node;
}
end->next=NULL;
spnt(h);
return h;
}
下面是txt文件中的一部分数据
7892501 4.268284 4.597226 5.157926 2.405777 3.193648 3.754392 5.296779 4.926208 4.193725 5.507898 3.917989 4.575308 3.389502 5.255974 5.330443
7892502 4.301623 4.658346 4.329074 3.959646 3.35032 4.880316 3.299998 4.118061 4.484093 4.775192 4.149987 4.322554 3.561494 4.177059 4.154296
7892503 1.977811 1.997611 2.046859 2.049589 2.792515 1.79253 2.024541 2.224103 1.797157 1.92102 2.950698 2.273576 3.028271 2.496572 2.026492
7892504 7.172867 7.288178 6.530507 7.212357 7.255197 7.339388 6.632795 6.730387 7.041014 6.297472 7.213027 6.831325 7.555257 6.78259 6.75404
7892505 1.616885 1.713534 1.429348 1.641011 1.684745 1.851415 1.190501 1.662887 1.665735 1.513973 2.312342 1.477317 2.311823 1.651856 2.515843
7892506 4.540164 5.406911 4.757912 4.769162 4.710423 4.033385 4.710111 4.111657 4.277024 4.546098 4.621146 4.479112 4.869566 5.531978 5.302863
7892507 3.383238 3.73878 3.17497 4.575585 3.716279 3.086894 2.459944 3.070731 4.419881 3.722239 2.561623 3.132647 2.908724 2.440594 2.921262
7892508 5.946597 6.713865 6.130948 7.061516 5.266613 5.562837 5.424371 5.622719 5.826248 6.140746 5.813846 6.038242 5.55389 5.877461 5.679112
7892509 10.18068 10.68538 10.56327 10.49562 10.31748 9.847937 10.25275 10.22152 10.25712 10.54865 10.28724 10.36604 9.800262 10.84328 10.51005
7892510 3.447968 3.736764 4.082025 4.312616 4.052792 3.679199 3.934424 3.939696 3.044951 4.575237 3.297375 3.786629 4.279774 4.030397 4.112758
7892511 2.686671 2.741276 2.472898 2.981007 2.568199 2.320586 3.037794 2.096805 2.441707 2.525166 2.785656 2.534265 3.339399 2.165352 2.005234
7892512 3.336195 4.584577 4.22262 2.954075 4.707194 4.192288 3.990947 4.627522 3.023889 3.53958 4.899905 3.928568 4.102745 4.013997 4.163525
7892513 3.450377 3.088314 3.029684 3.021764 3.027021 2.868983 3.815872 3.440092 3.858353 3.630024 1.991959 3.040341 2.931339 3.580529 3.088707
7892514 10.26685 10.48485 10.15717 10.59589 10.33332 10.50087 10.39478 10.34047 10.2488 10.05095 10.51089 10.21522 10.40543 10.18184 10.12425
7892515 6.686758 7.17328 6.511245 7.245693 6.993473 6.990703 6.771949 6.854737 6.74302 6.298997 7.147911 6.897004 6.507236 6.264549 6.090431
7892516 6.857567 6.650571 6.103684 6.753803 6.404892 6.066602 5.93637 6.02988 6.098182 5.702642 6.248299 6.003806 5.514124 6.176023 5.875672
问题:只能读入第一行数据
目标:将数据存入数组中