Reputation:
I am trying to make this simple hospital management system for my school project in C, when i try to edit the existing patient details by searching with their id number, That patient information gets edited, patient id,name etc but the problem is all other patients information that i don't want to edit becomes blank(means in place of id it becomes 0 and other remains blank except the edited patients details).
How do i edit only the details of patient i want to edit??
My code for editing
fp is my original filename and temp is temporary file i have created
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<process.h>
typedef struct Hospital{
int patientId;
char fname[20];
char lname[20];
char gender[10];
int age;
char ReferredTo[30];
}Hospital;
Hospital hospi;
void Record();
void Display();
void Add();
void Edit();
void Search();
void Delete();
int main(){
int ch;
top:
printf("\n1.Create Record of patients");
printf("\n2.Display Record of patients");
printf("\n3.Append Record of Additional patients");
printf("\n4.Edit the record of the patients");
printf("\n5.Search record of the patients");
printf("\n6.Delete record of the patients");
printf("\n---------------------------------------");
printf("\nEnter your choice: ");
scanf("%d",&ch);
printf("\n---------------------------------------");
if(ch == 1){
Record();
}else if(ch == 2){
Display();
} else if(ch == 3){
Add();
//goto top;
}else if(ch == 4){
Edit();
}else if(ch == 5){
Search();
}else if(ch == 6){
Delete();
}else{
printf("Invalid Choice");
}
}
void Record(){
FILE *fp;
int n = 0,i;
fp = fopen("C:\\project\\record.txt","w");
if(fp == NULL){
printf("Couldn't Open the file");
}else{
printf("\nHow many Record You want to enter: ");
scanf("%d",&n);
for(i=0; i<n; i++){
printf("\nEnter Patient ID: ");
scanf("%d",&hospi.patientId);
printf("\nEnter Patient First Name: ");
scanf("%s",hospi.fname);
fflush(stdin);
printf("\nEnter Patient Last Name: ");
scanf("%s",hospi.lname);
printf("\nEnter Patient sex(M for male F for Female): ");
scanf("%s",hospi.gender);
printf("\nEnter Patient Age: ");
scanf("%d",&hospi.age);
printf("\nEnter the Doctor Name which the patients is refered to: ");
scanf("%s",hospi.ReferredTo);
fwrite(&hospi,sizeof(hospi),1,fp);
}
printf("\nData Successfully Recorded....:)");
}
fclose(fp);
}
void Display(){
FILE *fp;
fp = fopen("C:\\project\\record.txt","r");
if(fp == NULL){
printf("Error: can't open file");
}else{
while(fread(&hospi,sizeof(hospi),1,fp)==1){
printf("\nPatient Id = %d\n",hospi.patientId);
printf("Patient Fullname = %s %s\n",hospi.fname,hospi.lname);
fflush(stdin);
printf("Patient Gender: %s\n",hospi.gender);
printf("Patient Age = %d\n",hospi.age);
printf("Referred Doctorr = %s\n",hospi.ReferredTo);
printf("---------------------------------------\n");
}
}
fclose(fp);
}
void Add(){
FILE *fp;
char ch;
int n = 0,i;
fp = fopen("C:\\project\\record.txt","a");
if(fp == NULL){
printf("Error: can't open file");
}else{
//printf("\nHow many Additional patients you want to add: ");
//scanf("%d",&n);
printf("\nEnter Patient ID: ");
scanf("%d",&hospi.patientId);
printf("\nEnter Patient First Name: ");
scanf("%s",hospi.fname);
fflush(stdin);
printf("\nEnter Patient Last Name: ");
scanf("%s",hospi.lname);
printf("\nEnter Patient sex(M for male F for Female): ");
scanf("%s",hospi.gender);
printf("\nEnter Patient Age: ");
scanf("%d",&hospi.age);
printf("\nEnter the Doctor Name which the patients is refered to: ");
scanf("%s",hospi.ReferredTo);
fwrite(&hospi,sizeof(hospi),1,fp);
}
printf("\nDo you want to continue adding data press('y' for yes and 'N' for no ) ");
scanf("%s",ch);
while(ch =='y'){
Add();
}
fclose(fp);
}
void Edit(){
int Pid;
FILE *fp,*temp;
fp = fopen("C:\\project\\record.txt","r");
temp = fopen("C:\\project\\temp.txt","a");
Hospital hospt;
printf("\nEnter the patient ID you want to Edit the data: ");
scanf("%d",&Pid);
while(fread(&hospt,sizeof(hospt),1,fp)==1){
if(Pid == hospt.patientId){
printf("\nEnter Patient ID: ");
scanf("%d",&hospi.patientId);
printf("\nEnter Patient First Name: ");
scanf("%s",hospi.fname);
fflush(stdin);
printf("\nEnter Patient Last Name: ");
scanf("%s",hospi.lname);
printf("\nEnter Patient sex(M for male F for Female): ");
scanf("%s",hospi.gender);
printf("\nEnter Patient Age: ");
scanf("%d",&hospi.age);
printf("\nEnter the Doctor Name which the patients is refered to: ");
scanf("%s",hospi.ReferredTo);
fwrite(&hospi,sizeof(hospi),1,temp);
}else{
fwrite(&hospi,sizeof(hospi),1,temp);
}
}
fclose(fp);
fclose(temp);
remove("C:\\project\\record.txt");
rename("C:\\project\\temp.txt","C:\\project\\record.txt");
}
void Search(){
FILE *fp;
int found_name = 0;
char search_name[20];
printf("\nEnter the Patient name you want to search for: ");
scanf("%s",search_name);
fp = fopen("C:\\project\\record.txt","r");
while(fread(&hospi,sizeof(hospi),1,fp)==1){
if(strcmp(search_name,hospi.fname)==0){
found_name++;
printf("\nPatient Id = %d\n",hospi.patientId);
printf("Patient Fullname = %s %s\n",hospi.fname,hospi.lname);
fflush(stdin);
printf("Patient Gender: %s\n",hospi.gender);
printf("Patient Age = %d\n",hospi.age);
printf("Referred Doctorr = %s\n",hospi.ReferredTo);
break;
}
}
if(found_name==0){
printf("\nThe Patient Name you searched for is not register in our system...:(");
}
fclose(fp);
}
void Delete(){
FILE *fp,*temp;
Hospital hospt;
int delete_id,found = 0;
fp = fopen("C:\\project\\record.txt","r");
temp = fopen("C:\\project\\temp.txt","a");
printf("\nEnter the patient id you want to Delete: ");
scanf("%d",&delete_id);
while(fread(&hospt,sizeof(hospt),1,fp)==1){
if(delete_id != hospt.patientId){
fwrite((&hospt),sizeof(hospt),1,temp);
}else{
found++;
}
}
fclose(fp);
fclose(temp);
remove("C:\\project\\record.txt");
rename("C:\\project\\temp.txt","C:\\project\\record.txt");
if(found>0)
{
printf("Data found and delete successfully\n");
}else{
printf("The Patiend id you want to delete is not in the File");
}
}
Upvotes: 0
Views: 147
Reputation: 108968
You're writing hospi
whether you update it or not; both if
part and else
part. You may want to write hospi
when matching input, hospt
otherwise.
while (fread(&hospt, sizeof(hospt), 1, fp) == 1) { // ^^^^^ if (Pid == hospt.patientId) { // ^^^^^ printf("\nEnter Patient ID: "); scanf("%d", &hospi.patientId); // ^^^^^ /* ... */ fwrite(&hospi, sizeof(hospi), 1, temp); // ^^^^^ } else { fwrite(&hospi, sizeof(hospi), 1, temp); // ^^^^^ } }
Upvotes: 1