1
void patient_data(){    

    char name[10];
    int ID[4];
    int age;
    char history[100];
    float temp;
    int breath; 

    printf("enter patient data:\n");    

    scanf("name : %s\n", name);
    scanf("ID: %d\n",ID);
    scanf("age %d\n",&age);
    scanf("history: %s\n", history);
    scanf("temp: %f\n",&temp);
    scanf("breath: %s\n", breath);

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

    fscanf(info,"%s     %d  %d  %s  %f  %d",name, ID, &age, history, &temp,&breath);
}

this code is supposed to take user input for patient data and save it in a file that should be accessed later, but the scanf functions are not working.

any Idea on what might be the issue here?

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 6
    *...are not working* Please elaborate. – lurker Jun 07 '20 at 22:58
  • 3
    This reads the data from the file, not from user input. If you wanted to take user input and then save it to a file, you would `scanf` it from the user and then `fprintf` it to the file. – nanofarad Jun 07 '20 at 22:58
  • 1
    You open `info` for writing and then use a read function (fscanf) – M.M Jun 07 '20 at 23:23

1 Answers1

3

Your scanf's are ill-formed, they should simply be:

scanf(" %9s", name); //<-- added buffer limit
scanf("%d",ID);
scanf("%d",&age);
scanf(" %99s", history); //<-- added buffer limit
scanf("%f",&temp);
scanf("%d", &breath);` // <-- corrected the specifier

If you want to print tags for the user to know what to input use printf or puts before every scanf.

Note that the last scanf has a specifier missmatch, it takes an integer but uses a string specifier.

Also note that the "%s" specifier is unsafe, you should use "%99s" for a 100 char container to avoid buffer overflow. The way you have it, it's no better than gets().

Finally the correct function to write to a file is fprintf.

fprintf(info,"%s     %d  %d  %s  %f  %d", name, ID, age, history, temp, breath);
anastaciu
  • 23,467
  • 7
  • 28
  • 53