输入后程序崩溃

时间:2020-06-08 06:25:51

标签: c

#define _CRT_SECURE_NO_WARNINGS
#include<Stdio.h>

/*
Covid-19 viral has effected so many countries around the world. The corona virus effected patients are kept in isolation centers for quarantine.
 There is need of computerization of health records. Write a program to design patient profile structure. The members of structure include patient name,
  age, gender, date of birth, city. Take another nested structure History that contain members such as patient register id, ward no, bed no, date from,
   date to details. Create a file patientRecord.txt and write content of structure into file. The next step is read the file content and print on console.
*/

FILE* prp;

struct pateint_profile {
    int register_id;
    int ward_no;
    int bed_no;
    char  d_o_a[10];
    char  d_o_d[10];



};
struct reocrd {
    char patient_name[20];
    int patient_age;
    char patient_gender[20];
    int d_o_b[10];
    char city[20];

    struct pateint_profile recrd;
}rcd;
int main()
{

    fopen("covid.txt", "w");
    if (prp == NULL)
    {
        printf("\n >> enter employee info<< ");

        printf("\n Enter Patient_Name :");
        scanf("%s", &rcd.patient_name);

        printf("\n Enter Patient_age :");
        scanf("%d", &rcd.patient_age);

        printf("\n Enter Patient_Gender:");
        scanf("%s", &rcd.patient_gender);

        printf("\n Enter Patient_DATE_OF_BIRTH :");
        scanf("%s", &rcd.d_o_b);

        printf("\n Enter Patient_City:");
        scanf("%s", &rcd.city);

        printf("\n Enter Patient_Registration_NO :");
        scanf("%d", &rcd.recrd.register_id);

        printf("\n Enter Patient_Ward_NO :");
        scanf("%d", &rcd.recrd.ward_no);

        printf("\n Enter Patient_DATE_OF_ADMIT :");
        scanf("%s", &rcd.recrd.d_o_a);

        printf("\n Enter Patient_DATE_OF_DISCHARGE :");
        scanf("%s", &rcd.recrd.d_o_d);



        printf("\n Printing the info of COVID_PATIENT:");
        printf("\n Patient_Name: %s \n Patient_age %d \n Patient_Gender %s \n Patient_DATE_OF_BIRTH %s \n Patient_City: %s \n Patient_Registration_NO: %d , \n Patient_Ward_NO: %d,\n Patient_DATE_OF_ADMIT: %s, \n Patient_DATE_OF_DISCHARGE: %s",
            rcd.patient_name, rcd.patient_age, rcd.patient_gender, rcd.d_o_b, rcd.city, &rcd.recrd.register_id, rcd.recrd.ward_no, rcd.recrd.d_o_a, rcd.recrd.d_o_d);
    }
    else
    {
        printf("the file  is empty");
    }
    fclose (prp);
    return 0;
}

我有一项任务要记录我正在使用FILE的covid-19患者,但是当我运行该程序时,它从用户那里获取输入,但是在显示信息之后,它崩溃并显示运行时库错误,并且记录也未保存在文件中,是由于在开始时初始化了FILE *还是其他原因?谢谢。

2 个答案:

答案 0 :(得分:2)

fopen("covid.txt", "w");

您尝试打开covid.txt,但忽略操作结果。

之后fclose个无效(未初始化)的文件句柄

fclose(prp);

还...

w清除文件内容(如果存在)。

•因为您不限制读取的字符数,所以缓冲区可能会溢出。例如scanf("%s", &rcd.city);应该为scanf("%19s", &rcd.city);(因为city最多可以包含19个字符)

•您不应该检查scanf的返回值。


回答如何写入文件:

#include <stdio.h> // printf, fopen, fprintf, fclose
#include <stdlib.h> // EXIT_SUCCESS, EXIT_FAILURE

int main() {
    FILE *fp;

    // Attempt to open file "test.txt"
    fp = fopen("test.txt", "a+");

    // fopen() returns NULL on error
    // so we check if an error occurred
    if (fp == NULL) {
        printf("Error opening file.\n");
        // terminate main()
        return EXIT_FAILURE;
    }

    // we have a valid file handle
    // now we can use something like
    // fprintf to write to the file.
    int bytes_written = fprintf(fp, "Hello, %s!\n", "World");

    if (0 > bytes_written) {
        printf("Error writing to file.\n");
        fclose(fp);
        return EXIT_FAILURE;
    }

    printf("Successfully wrote to file!\n");

    // close file handle
    fclose(fp);

    return EXIT_SUCCESS;
}

答案 1 :(得分:1)

这些是您应该在代码中进行的更改:

FILE* prp = fopen("covid.txt","a");

scanf之后:

fprintf(prp, "%s", <scannedvaluehere>);

@ marco-a已经提到了其余部分

相关问题