ZhTioo 2021-12-23 12:15 采纳率: 66.7%
浏览 23
已结题

将输出数据保存到文本文件

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ITEM "Song management system"

struct song *shuru(struct song **head);
void charu(struct song **head);
void shanchu(struct song **head);
void liulan(struct song **head);
void xiugai(struct song **head);
void creat(struct song **head);

struct date
    {
        int year;
        int month;
    }tis;
struct song  //声明结构体类型struct song
    {
        char music[20];
        char company[20];
        char author[20];
        char singer[20];
        struct date tis;
        struct song *next;
    };

int main()  //对所需进行操作的选择
{
    struct song *shuru(struct song **head);
    void charu(struct song **head);
    void shanchu(struct song **head);
    void liulan(struct song **head);
    void xiugai(struct song **head);
    void creat(struct song **head);
    void fenzu(struct song **head); 

    struct song *head;  //定义一个结构体类型的指针作为实参 
    int k,w1;
    char ch;
    z:
    while(k!=7)
    {
        do{
        printf("**********歌曲信息管理系统*********\n\n");
        printf("1.输入\n");
        printf("2.插入\n");
        printf("3.删除\n");
        printf("4.浏览\n");
        printf("5.修改\n");
        printf("6.分组\n");
        printf("7.退出\n");
        printf("请选择你所需的功能:");
        scanf("%d",&k);
        if(k<1&&k>7)
            w1=1;
        else w1=0;
        }
    while(w1==1);
    switch(k)
    {
        case 1 :
            //system("cls");
            shuru(&head);
            printf("按z返回主菜单:");
            scanf("%c",&ch);
            if(ch=='z')
            {
                goto z;
            }
            break;
        case 2 :
            system("cls");
            charu(&head);
            printf("按z返回主菜单:");
            scanf("%c",&ch);
            if(ch=='z')
            {
                goto z;
            }
            break;
        case 3 :
            system("cls");
            shanchu(&head);
            printf("按z返回主菜单:");
            scanf("%c",&ch);
            if(ch=='z')
            {
                goto z;
            }
            break;
        case 4 :
            system("cls");
            liulan(&head);
            printf("按z返回主菜单:");
            scanf("%c",&ch);
            if(ch=='z')
            {
                goto z;
            }
            break;
        case 5 :
            system("cls");
            xiugai(&head);
            printf("按z返回主菜单:");
            scanf("%c",&ch);
            if(ch=='z')
            {
                goto z;
            }
            break;
        case 6 :
            system("cls");
            fenzu(&head);
            printf("按z返回主菜单:");
            scanf("%c",&ch);
            if(ch=='z')
            {
                goto z;
            }
            break;
        case 7 :exit(0);
    }
    }
}

void creat(struct song **head)
{
    struct song *p=(*head)->next;
    int j=1;
    if(p == NULL)
    {
        printf("保存失败");
    }
    FILE *fp=fopen(ITEM,"w");
    if(fp == NULL)
    {
        printf("打开文件失败\n");
        return; 
    }
    while(p!=NULL)
    {
        fprintf(fp,"第%d首歌的信息如下:\n",j);
        fprintf(fp,"名字  发行公司    作者   歌手  发行年 发行月\n");
        fprintf(fp,"%s %s %s %s %d %d \n",p->music,p->company,p->author,p->singer,p->tis.year,p->tis.month);
        j++;
        p=p->next;
    }
    fclose(fp);
}

struct song *shuru(struct song **head)  //对歌曲的输入
{
    printf("***********输入**********\n");
    int i,n;
    *head = (struct song *)malloc(sizeof(struct song));
    struct song *p=*head, *shang;
    shang = (struct song *)malloc(sizeof(struct song));
    (*head)->next=NULL;
    printf("准备输入歌曲的数目:");
    scanf("%d",&n);
    printf("输入歌曲信息:\n");
    for(i = 1;i<=n;i++)
    {
        p=(struct song *)malloc(sizeof(struct song));
        shang->next = p;
        printf("输入第%d歌曲的名字:",i);
        scanf("%s",p->music);
        printf("输入第%d歌曲的发行公司:",i);
        scanf("%s",p->company);
        printf("输入第%d歌曲的作者:",i);
        scanf("%s",p->author);
        printf("输入第%d歌曲的歌手:",i);
        scanf("%s",p->singer);
        printf("输入第%d歌曲的发行年:",i);
        scanf("%d",(&p->tis.year));
        printf("输入第%d歌曲的发行月:",i);
        scanf("%d",(&p->tis.month));
        (*head)->next=p;
        shang = p;
    }
    printf("输入结束\n");
    creat(head);
    printf("保存成功!\n");
    return *head;
}

void charu(struct song **head)  //插入歌曲信息
{
    void liulan(struct song **head);
    printf("***********插入**********\n");
    int i,j=1;
    struct song *q,*p=(*head)->next;
    printf("输入要插入歌曲的位置:\n");
    scanf("%d",&i);
    while(p->next&&j<i)
    {
        p=p->next;
        j++;
    }
    if(p->next == NULL||j>i)
        printf("未找到该结点\n");
    else
    {
        q=(struct song *)malloc(sizeof(struct song));
        printf("输入插入歌曲的名字:");
        scanf("%s",q->music);
        printf("输入插入歌曲的发行公司:");
        scanf("%s",q->company);
        printf("输入插入歌曲的作者:");
        scanf("%s",q->author);
        printf("输入插入歌曲的歌手:");
        scanf("%s",q->singer);
        printf("输入插入歌曲的发行年:");
        scanf("%d",&(q->tis.year));
        printf("输入插入歌曲的发行月:");
        scanf("%d",&(q->tis.month));
        q->next=p->next;
        p->next=q;
    }
    printf("插入后的目录为\n");
    liulan(head); 
}

void shanchu(struct song **head)  //删除某个歌曲信息
{
    void liulan(struct song **head); 
    printf("***********删除**********\n");
    int i,j=1;
    struct song *q,*p=(*head)->next;
    printf("输入要删除歌曲的位置:\n");
    scanf("%d",&i);
    while(p&&j<i)
    {
        p=p->next;
        j++;
    }
    if(p->next==NULL||j>i)
        printf("没有找到要删除歌曲的位置\n");
    else
    {
        q=(struct song *)malloc(sizeof(struct song));
        q=p->next;
        p->next=q->next;
        free(q);
    }
    printf("删除后的目录为\n");
    liulan(head); 
}

void liulan(struct song **head)  //浏览目录中的歌曲
{
    printf("***********歌曲**********\n");
    int j=1;
    struct song *p=(*head)->next;
    while(p->next!=NULL)
    {
        printf("第%d首歌的信息如下:\n",j);
        printf("名字  发行公司    作者   歌手  发行年 发行月\n");
        printf("%s %s %s %s %d %d \n",p->music,p->company,p->author,p->singer,p->tis.year,p->tis.month);
        j++;
        p=p->next;
    }
}

void xiugai(struct song **head)  //修改歌曲信息
{
    void liulan(struct song **head);
    printf("***********修改**********\n");
    int j=1,t;
    struct song *p=(*head)->next;
    printf("输入要修改歌曲的位置:\n");
    scanf("%d",&t);
    while(p&&j<t)
    {
        p=p->next;
        j++;
    }
    if(p==NULL||j>t)
        printf("未找到要修改的歌曲\n");
    else
    {
        printf("输入修改后歌曲的名字:\n");
        scanf("%s",p->music);
        printf("输入修改后歌曲的发行公司:\n");
        scanf("%s",p->company);
        printf("输入修改后歌曲的作者:\n");
        scanf("%s",p->author);
        printf("输入修改后歌曲的歌手:\n");
        scanf("%s",p->singer);
        printf("输入修改后歌曲的发行年:\n");
        scanf("%d",p->tis.year);
        printf("输入修改后歌曲的发行月:\n");
        scanf("%d",p->tis.month);
    }
    printf("修改后的目录为\n");
    liulan(head); 
}

void fenzu(struct song **head)
{
    printf("***********分组**********\n");
    int i,j,n,m,t=1;
    char a[20]="\0";
    struct song *p=(*head)->next;
    printf("1.按歌手分组\n");
    printf("2.按发行公司分组\n");
    printf("请输入要分组的类型:");
    scanf("%d",&j);
    printf("请输入分组前几首歌:");
    scanf("%d",&n);
    printf("请输入分组信息:");
    scanf("%s",a);
    switch(j)
    {
        case 1:
            for(i=0;i<n;i++)
           {
               if(strcmp(p->singer,a)==0)
               {
                 printf("第%d首歌的信息为:\n",t);
                  printf("%s %s %s %s %d %d\n",p->music,p->singer,p->company,p->author,p->tis.year,p->tis.month);
                  t++;
                p=p->next;
            }break;
        }
        case 2:
            for(i=0;i<n;i++)
        {
            if(strcmp(p->company,a)==0)
            {
                printf("第%d首歌的信息为:\n",t);
                printf("%s %s %s %s %d %d\n",p->music,p->singer,p->company,p->author,p->tis.year,p->tis.month);
                t++;
                p=p->next;
            }
        }
    }
}







为什么不能保存到文件
求指教

  • 写回答

1条回答 默认 最新

  • CCodelab 2021-12-23 13:15
    关注

    能保存到文件,是保存不完全,你检查一下链表的操作

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。