#include<stdio.h>
#include <stdlib.h>
#include <string.h>
struct _PACKAGE
{
int no;//快递单号
char name[20];//收货人
char tel[20];//收货人号码
char add[40];//收货人地址
char cate[10];//运单种类
char com[10];//承运公司
char time[20];//到达时间
struct _PACKAGE *next;
};
FILE *fp;
struct _PACKAGE *p1;
void creat()
{
int a;
struct _PACKAGE *s,*head,*tail;
head=NULL;
tail=NULL;
system("cls");
if((fp=fopen("E:\程序\快递包裹管理系统作业\record.txt","w"))==NULL)
{
printf("打开文件失败");
}
do
{
s=(struct _PACKAGE *)malloc(sizeof(struct _PACKAGE));//开辟新链表
printf("快递单号:");
scanf("%d",&s->no);
printf("\n收货人:");
scanf("%s",s->name);
printf("\n收货人号码:");
scanf("%s",s->tel);
printf("\n收货人地址:");
scanf("%s",s->add);
printf("\n运单种类:");
scanf("%s",s->cate);
printf("\n乘运公司:");
scanf("%s",s->com);
printf("\n到达时间:");
scanf("%s",s->time);
if(head==NULL) head=s;
else tail->next=s;
tail=s;
fwrite(s,sizeof(struct _PACKAGE ),1,fp);
//fprintf(fp,"%d\t%s\t%s\t\t%s\t\t%s\t%s\t%s\n",s->no,s->name,s->tel,s->add,s->cate,s->com,s->time);
printf("继续请输入任意非零数字||退出请输入0\n");
scanf("%d",&a);
system("cls");
}while(a!=0);
fclose(fp);
}
int Print()
{
system("cls");
if ((fp=fopen("E:\\程序\\快递包裹管理系统作业\\record.txt","rb"))==NULL)
{
printf("不能打开文件,按任意键退出......\n");
system("pause");
}
printf("单号 | 收货人 |收货人号码|收货人地址|运单种类| 承运公司 | 到达时间\n");
printf("------------------------------------------------------------------------\n");
p1=(struct _PACKAGE *)malloc(sizeof(struct _PACKAGE));
do
{
fread(p1,sizeof(struct _PACKAGE),1,fp);
if(fread(p1,sizeof(struct _PACKAGE),1,fp)!=1)
{
if(feof(fp))
{
printf("输出结束!");
system("pause");
break;
}
}
printf("%6s%6s%6s%10s%6s%10s%10s\n",p1->no,p1->name,p1->tel,p1->add,p1->cate,p1->com,p1->time);
}while(1);
fclose(fp);
return 0;
}
int main()
{
creat();
Print();
return 0;
}