#include <stdio.h>
#include <stdlib.h>
#define MAX 50
typedef struct
{
int month;
int day;
double money;
char shop[50];
} datatype1;
typedef struct node1
{
datatype1 data;
struct node1 *next;
} LinkNode1;
typedef struct
{
int yf;/*月份*/
double spxf; /*食品消费*/
LinkNode1* firstbill;//销账
double fz; /*房租*/
double znjy;/*子女教育费用*/
double sdf; /*水电费*/
double ylf;/*医疗费*/
double cx;/*储蓄*/
} datatype;
typedef struct node
{
datatype data;
struct node *next;
} LinkNode;
LinkNode* initList()
{
LinkNode* p;
p=( LinkNode*)malloc(sizeof(LinkNode));
p->next=NULL;
return p;
}
void createList(LinkNode* head)
{
FILE *fp;
datatype stu;
LinkNode* p, *rear;
if((fp=fopen("records.txt","r"))==NULL)
{
printf("can not open read file !\n");
exit(1); /*返回OS,该函数定义在stdlib.h中*/
}
while(!feof(fp))
{
fscanf(fp,"%d %lf %lf %lf %lf %lf %lf",&stu.yf,&stu.spxf,&stu.fz,&stu.znjy,&stu.sdf,&stu.ylf,&stu.cx);
p=( LinkNode*)malloc(sizeof(LinkNode));
p->data=stu;
p->data.firstbill=NULL;
p->next=NULL;
rear->next=p;
rear=p;
}
fclose(fp);
}
void pntList(LinkNode* head)
{
printf(" 月份 食品消费 房租 子女教育费用 水电费 医疗费 储蓄\n");
LinkNode* d;
d=head->next;
while (d!=NULL)
{
printf("%5.0d月 %5.2f元 %5.2f元 %5.2f元 %5.2f元 %5.2f元 %5.2f元\n",d->data.yf,d->data.spxf,d->data.fz,d->data.znjy,d->data.sdf,d->data.ylf,d->data.cx);
d=d->next;
}
}
int main()
{
LinkNode* p;
p=initList();
createList(p);
pntList(p);
return 0;
}
records.txt里
1 1 2 3 4 5 6 7
2 111 222 333 444 555 666 777
3 11 22 33 44 55 66 77
就是我想在结构体里再加一串数据,就在什么月份 食品消费 房租 子女教育费用 水电费 医疗费 储蓄这里面加个收入
但我double shouru; txt每行尾部加了串数据然后在fscanf里面也加了 %lf和&stu.shouru之后 直接return 乱码了。
想了半天 实在是不懂为什么会出读不出来数据 问一问