LYQZADM16 2021-12-14 21:05 采纳率: 80%
浏览 24
已结题

为什么我这个代码无法写入文件里面?

问题遇到的现象和发生背景

无法写入文件

问题相关代码,请勿粘贴截图
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define M 100

struct data
{
    char year;
    char month;
    char day;
};



struct stud
{
    struct data time;
    char whichsection;
    char lesson;
    char name[M];
    char type;
};
struct stud arrange[M];

FILE *fp;
int n = 0;
void menu();
void function1();
/*void function2();
void function3();
void function4();
void function5();
void function6();
void function7();*/


void main()
{
    menu();
}
/***********************************************menu()***********************************************************/
void menu()
{
    int choice;
    printf("------------------------------------------------------------------------------------------------\n");
    printf("||~~~***The student attendance information***~~~||\n");
    printf("================================================================================================\n");
    printf("||1.input       the information                                       2.browse the imformation||\n");
    printf("||3.aad         the imformation                                       4.delete the infoemation||\n");
    printf("||5.search      the information                                       6.amendant  the   record||\n");
    printf("||7.statistical the information                                       0.exit      the   system||\n");
    printf("================================================================================================\n");
    printf("| The ueser can make a choice form the funtion above |\n");
    printf("------------------------------------------------------------------------------------------------\n");
    scanf("%d", &choice);
    switch (choice)
    {
    case 1:function1(); break;
        /*case'2':function2(); break;
        case'3':function3(); break;
        case'4':function4(); break;
        case'5':function5(); break;
        case'6':function6(); break;
        case'7':function7(); break;*/
    case 0:exit(0);
    }
    menu();

}
/**********************************************input the imformation***********************************************/
void function1()
{
    int a, n;
    if ((fp = fopen("E:\\data\\info.txt", "w")) == NULL)
    {
        printf("can't open the file");
        exit(1);
    }

    for (n = 1; n <= M; n++)
    {
        printf("input the data:");
        scanf("%s %s %s", &arrange[n].time.year, &arrange[n].time.month, &arrange[n].time.day);

        printf("input what the student name is:");
        scanf("%s", &arrange[n].name[100]);

        printf("input which section:");
        scanf("%s", &arrange[n].whichsection);
        
        printf("input what the lesson is:");
        scanf("%s", &arrange[n].lesson);

        printf("input what the type is:");
        scanf("%s", &arrange[n].type);

        printf("If you want to input more,press 1 ,or press 0\n");
        scanf("%d", &a);
        if (a == 0)
            break;
        if (a == 1)
            printf("\n");
    }
    if (fread(&arrange[n], sizeof(struct stud), n, fp) != n)
    {
        printf("you can't write the file\n");
        exit(1);
    }
    fclose(fp);
}
    

运行结果及报错内容

you can't write the file

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

1条回答 默认 最新

  • qzjhjxj 2021-12-15 14:27
    关注

    修改如下,见注释,供参考:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define M 100
    struct data
    {
        char year[5]; //修改
        char month[3];//修改
        char day[3];  //修改
    };
    struct stud
    {
        struct data time;
        char whichsection[16];//修改
        char lesson[16];      //修改
        char name[M];         //修改
        char type[16];        //修改
    };
    struct stud arrange[M];
    FILE* fp;
    int n = 0;
    void menu();
    void function1();
    /*void function2();
    void function3();
    void function4();
    void function5();
    void function6();
    void function7();*/
    
    void main()
    {
        menu();
    }
    /***********************************************menu()***********************************************************/
    void menu()
    {
        int choice;
        printf("------------------------------------------------------------------------------------------------\n");
        printf("                       ||~~~***The student attendance information***~~~||                       \n");//修改
        printf("================================================================================================\n");
        printf("||1.input       the information                                       2.browse the imformation||\n");
        printf("||3.aad         the imformation                                       4.delete the infoemation||\n");
        printf("||5.search      the information                                       6.amendant  the   record||\n");
        printf("||7.statistical the information                                       0.exit      the   system||\n");
        printf("================================================================================================\n");
        printf("| The ueser can make a choice form the funtion above |\n");
        printf("------------------------------------------------------------------------------------------------\n");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:function1(); break;
            /*case'2':function2(); break;
            case'3':function3(); break;
            case'4':function4(); break;
            case'5':function5(); break;
            case'6':function6(); break;
            case'7':function7(); break;*/
        case 0:exit(0);
        }
        menu();
    }
    /**********************************************input the imformation***********************************************/
    void function1()
    {
        int a = 1;   // n; //修改
        if ((fp = fopen("E:\\data\\info.txt", "w")) == NULL)
        {
            printf("can't open the file");
            exit(1);
        }
        for (n = 0; n < M && a != 0; n++) //for (n = 1; n <= M; n++) //修改
        {
            printf("input the data:");
            scanf("%s %s %s", arrange[n].time.year, arrange[n].time.month, arrange[n].time.day); //修改
            //canf("%s %s %s", &arrange[n].time.year, &arrange[n].time.month, &arrange[n].time.day);
            printf("input what the student name is:");
            scanf("%s", arrange[n].name);         //修改
            //scanf("%s", &arrange[n].name[100]);
            printf("input which section:");
            scanf("%s", arrange[n].whichsection);  //修改 
            //scanf("%s", &arrange[n].whichsection);
            printf("input what the lesson is:");
            scanf("%s", arrange[n].lesson);       //修改
            //scanf("%s", &arrange[n].lesson);
            printf("input what the type is:");
            scanf("%s", arrange[n].type);         //修改
            //scanf("%s", &arrange[n].type);
            printf("If you want to input more,press 1 ,or press 0\n");
            scanf("%d", &a);
            //if (a == 0)     //修改
            //    break;
            //if (a == 1)
            //    printf("\n");
        }
        if (fwrite(arrange, sizeof(struct stud), n, fp) != n)
            //fread(&arrange[n], sizeof(struct stud), n, fp) != n
        {
            printf("you can't write the file\n");
            exit(1);
        }
        fclose(fp);
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月26日
  • 已采纳回答 12月18日
  • 创建了问题 12月14日

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?