辞旧690 2022-01-02 21:56 采纳率: 75%
浏览 25
已结题

为何无法从文件读取并显示信息

#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;
}

  • 写回答

2条回答 默认 最新

  • 书山客 2022-01-02 22:17
    关注
    
    #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("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("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("%6d%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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月10日
  • 已采纳回答 1月2日
  • 创建了问题 1月2日

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?