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");
}
}
}