实验作业结构体和文件|C璇瑷浣滀笟鈥︹﹀皬鐧界粰璺浜嗐

① C璇瑷浣滀笟鈥︹﹀皬鐧界粰璺浜嗐

寰堢畝鍗曠殑浣滀笟锛岃嚜宸卞ソ濂界爺绌朵笅鎴戠粰鐨勭▼搴忥紝缁撴瀯浣撳拰鏂囦欢璇诲啓寰堥噸瑕佺殑

#include <stdio.h>

#include<stdlib.h>

#include<windows.h>

typedef struct city_info

{

char name[20];

int num;

int temp;

int wind;

int pm;

city_info *next;

}city,*pcity;

//鍐欏叆鏂囦欢

void WriteFile(pcity &L)

{

FILE *fp;

pcity p;

p=L->next;

if((fp=fopen("city.dat","wt+"))==NULL)

{

printf("鎵撳紑鏂囦欢澶辫触!");

exit(1);

}

while(p)

{

fprintf(fp,"鍚嶇О:");

fputs(p->name,fp);

fprintf(fp,"鍖哄彿:%d姘旀俯:%d椋庨:%d绌烘皵璐ㄩ噺鎸囨暟:%d",p->temp,p->temp,p->wind,p->pm);

p=p->next;

}

printf("鎴愬姛鍐欏叆city.dat!");

fclose(fp);

}

//鍒涘缓琛

void CreateList(pcity &L)

{

int i;

city *p;

L=(pcity)malloc(sizeof(city));

L->next=NULL;

printf("杈撳叆鍗佷釜鍩庡競鍙婂叾鍖哄彿銆佹俯搴︺侀庨熴佺┖姘旇川閲忔暟鎹甛n");

for(i=0;i<10;i++)

{

p=(pcity)malloc(sizeof(city));

scanf("%s",&p->name);

scanf("%d",&p->num);

scanf("%d",&p->temp);

scanf("%d",&p->wind);

scanf("%d",&p->pm);

p->next=L->next;

L->next=p;

}

}

void main()

{

pcity p;

CreateList(p);

WriteFile(p);

}


赞 (0)