跪求~~~一道C语言编程题的答案~~~要程序代码~~
a.创建一个包含下列数据的文件。H.Baker614FreemanSt.OrangeNJD.Rosso83ChambersSt.MadisonNJK.Tims819Rid...
a.创建一个包含下列数据的文件。
H.Baker 614 Freeman St. Orange NJ
D.Rosso 83 Chambers St. Madison NJ
K.Tims 819 Ridgewood Rd. Millburn NJ
B.Williams 24Tremont Ave. Brooklyn NY
b.编写一个程序,利用下列输出格式读取和显示练习a中创建的数据文件
Name:
Address:
City,State: 展开
H.Baker 614 Freeman St. Orange NJ
D.Rosso 83 Chambers St. Madison NJ
K.Tims 819 Ridgewood Rd. Millburn NJ
B.Williams 24Tremont Ave. Brooklyn NY
b.编写一个程序,利用下列输出格式读取和显示练习a中创建的数据文件
Name:
Address:
City,State: 展开
2个回答
展开全部
#include "stdio.h"
struct student
{ char name[20];
char address[50];
char city[20];
char state[10];
}stu[4];
void input()
{ int i; FILE *fp;
if((fp=fopen("a.txt","r"))==NULL)
{ printf("cannot open file.\n");
exit(0); }
for(i=0;i<4;i++)
fscanf(fp,"%s %s %s %s",stu[i].name,stu[i].address,stu[i].city,stu[i].state);
fclose(fp);
}
void output()
{ int i;
printf(" name:");
for(i=0;i<4;i++)
printf(" %s\t ",stu[i].name);
printf("\naddress:");
for(i=0;i<4;i++)
printf(" %s",stu[i].address);
printf("\ncity,state:");
for(i=0;i<4;i++)
printf(" %s,%s\t",stu[i].city,stu[i].state);
}
void main()
{ input();
output();
}
结果是这样么:
name: H.Baker D.Rosso K.Tims B.Williams
address: 614FreemanSt. 83ChambersSt. 819RidgewoodRd. 24TremontAve.
city,state: Orange,NJ Madison,NJ Millburn,NJ Brooklyn,NY
有问题hi我
struct student
{ char name[20];
char address[50];
char city[20];
char state[10];
}stu[4];
void input()
{ int i; FILE *fp;
if((fp=fopen("a.txt","r"))==NULL)
{ printf("cannot open file.\n");
exit(0); }
for(i=0;i<4;i++)
fscanf(fp,"%s %s %s %s",stu[i].name,stu[i].address,stu[i].city,stu[i].state);
fclose(fp);
}
void output()
{ int i;
printf(" name:");
for(i=0;i<4;i++)
printf(" %s\t ",stu[i].name);
printf("\naddress:");
for(i=0;i<4;i++)
printf(" %s",stu[i].address);
printf("\ncity,state:");
for(i=0;i<4;i++)
printf(" %s,%s\t",stu[i].city,stu[i].state);
}
void main()
{ input();
output();
}
结果是这样么:
name: H.Baker D.Rosso K.Tims B.Williams
address: 614FreemanSt. 83ChambersSt. 819RidgewoodRd. 24TremontAve.
city,state: Orange,NJ Madison,NJ Millburn,NJ Brooklyn,NY
有问题hi我
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询