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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么