0

This is my code in file processing. the requirement is to create a C program that will store patients' personal and vaccination information to a file with the name Vaxx.txt, Import/Read a file and append it to Vaxx.txt, then display all records in Vaxx.txt before exiting the program. And I have a problem with importFile function, the importFile function has to read the file and append all of its content to Vaxx.txt.

#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>

FILE *myFile;

struct Px_info info;
struct Px_address address;
struct Px_ext_info extInfo;

struct Px_info{

char category[5];
char last_name[20];
char first_name[20];
char middle_name[20];
char contact_number[15];

}; struct Px_address{

char house_number[100];
char street[100];
char barangay[100];
char city[20];
char province[20];
    struct Px_ext_info{

        char sex[5];
        char date_of_birth[50];
        char vaccine_manufacturer[20];
        char vaccination_date[20];  
    }ext_info;

};

void getDetails(){

printf("Enter Category:\n");
gets(info.category);

printf("~~Enter your Personal Information~~\n");

printf("Last Name:\n");
gets(info.last_name);

printf("First Name:\n");
gets(info.first_name);

printf("Middle Name:\n");
gets(info.middle_name);

printf("Contact Number:\n");
gets(info.contact_number);

printf("\n~~Address~~\n");
    
printf("House Number:\n");
gets(address.house_number);

printf("Street:\n");
gets(address.street);

printf("Barangay:\n");
gets(address.barangay);

printf("City:\n");
gets(address.city);

printf("Province:\n");
gets(address.province);

printf("\nSex M/F:\n");
gets(extInfo.sex);

printf("Date of Birth MM/DD/YYYY:\n");
gets(extInfo.date_of_birth);

printf("\nVaccine Manufacturer:\n");
gets(extInfo.vaccine_manufacturer);
    
printf("Vaccination Date MM/DD/YYYY:\n");
gets(extInfo.vaccination_date);

myFile = fopen("Vaxx.txt", "a");

fprintf(myFile, "Category: %s", info.category);
fprintf(myFile, "\nLast Name: %s", info.last_name);
fprintf(myFile, "\nFirst Name: %s", info.first_name);
fprintf(myFile, "\nMiddle Name: %s", info.middle_name);
fprintf(myFile, "\nContact Number: %s\n", info.contact_number);
fputs("~~Address~~", myFile);
fprintf(myFile, "\nHouse Number: %s", address.house_number);
fprintf(myFile, "\nStreet: %s", address.street);
fprintf(myFile, "\nBarangay: %s", address.barangay);
fprintf(myFile, "\nCity: %s", address.city);
fprintf(myFile, "\nProvince: %s", address.province);
fprintf(myFile, "\nSex: %s", extInfo.sex);
fprintf(myFile, "\nDate of Birth: %s", extInfo.date_of_birth);
fprintf(myFile, "\nVaccine Manufacturer: %s", extInfo.vaccine_manufacturer);
fprintf(myFile, "\nVaccination Date: %s\n\n\n", extInfo.vaccination_date);

printf("\n\n~~RECORD SAVED SUCCESSFULLY~~\n");

fclose(myFile);

}

void importFile(){

char fname[10000], ch;
FILE *fp;
printf("Enter the Name of File:\n");
gets(fname);
fp = fopen(fname, "r");
if(fp==NULL)
    printf("Error Occurred while Opening the File!");
else
{
    printf("\nFile Imported Successfully");
}
fp = fopen(fname, "a+");

fclose(fp);

}

void displayRecords(){

FILE *fopen();
int c;
myFile = fopen("Vaxx.txt", "r+");
c = getc(myFile);
while(c != EOF){
    putchar(c);
c = getc(myFile);
}
fclose(myFile);

}

int main(){

int option;

while(option != 4){

printf("[1] - Add New Record\n");
printf("[2] - Import Record\n");
printf("[3] - Diplay All Record\n");
printf("[4] - Exit\n");
printf("What do you want to do? ");
scanf("%d", &option);

if(option == 1){
    
    getDetails();
    
    system("pause");
    system("cls");
    
    }else if(option == 2){
        
        importFile();
        
        system("pause");
        system("cls");
        
    }else if(option == 3){
        
        displayRecords();
        
        system("pause");
        system("cls");
        
    }else if(option == 4){
        
        printf("Closing Program\n");
        
        system("pause");
        system("cls");
        
    }else{
        
        printf("Invalid Input! Please try again\n");
        
        system("pause");
        system("cls");
    }
}

}

  • 1
    Not your actual problem but very important anyway: [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – kaylum Jun 02 '22 at 04:42
  • I even used fgets but still doesn't work – BSIT 1B Gacho Marc James A Jun 02 '22 at 04:50
  • 1
    `gets` vs `fgets` is not the cause of your problem but just pointing out that `gets` should never be used in any situation. The cause of your problem is explained in the second comment. – kaylum Jun 02 '22 at 04:51
  • okay.. I tried to use getchar, it didn't skip the next scanf, but it still skips some scanf – BSIT 1B Gacho Marc James A Jun 02 '22 at 05:07
  • Programming is a precise task. So explain your problem precisely. Where did you put the `getchar`, what is the exact input, which `scanf` are skipped? If you need further help suggest you post a new question. – kaylum Jun 02 '22 at 05:16
  • so, as you can see in my code, I'm doing a while loop, so when I enter 1 it executes the getDetails function, and there it will get now my personal information. But once I input my Category which is that first scanf and getchar, the next scanf and getchar which is the last name will be skipped and proceeds directly to first name. – BSIT 1B Gacho Marc James A Jun 02 '22 at 06:23
  • finally find it out, thanks by the way, sorry for taking a lot of your time. My mistake was, I didn't try to put a getchar after the scanf("%d", &option); now it working – BSIT 1B Gacho Marc James A Jun 02 '22 at 08:08

0 Answers0