The following code is an employee management system. There are four functions: insert, list, edit and exit. This issue is present at the void list function, line 148 a. It's not displaying the list of employees. It seems mostly correct to me. I ask if someone can please kindly assist?
This is the struct:
struct employee
{
char name[50];
char sex[7];
char adrs[50];
char dsgn[25];
int age,empID;
float slry;
};
List function:
void list ()
{
printf("-------------------------------");
printf("\nEmployee Details: \n---------------------------------\n");
rewind(fptr);///moves file to start of the file
while(fread(&e,recsize,1,fptr)==1)///read the file and fetch the record one record per fetch
{
printf("\n\n%s \t\t%6s \t%s \t%s \t%d \t%.2f \t%d",e.name, e.sex, e.adrs, e.dsgn, e.age,
e.slry, e.empID);
}
getch();
Entire code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <stdbool.h>
#include <windows.h>
#include "struct.h"
void insert();
void list();
void edit();
void del();
void exit();
int tolower();
FILE * fptr, *ftemp;
struct employee e;
long int recsize;
char empname[50];
int main()
{
int choice;
fptr = fopen("ems.txt", "r+");
if (fptr == NULL)
{
printf("Can't find file! Attempting to create file... \n");
fptr = fopen("ems.txt","w+");
if(fptr == NULL)
{
printf("Can't create file. Exiting...");
exit(1);
}
}
//Explain the reason for this?
//recsize = (long int) sizeof(e);//
while(1)
{
printf("*******************************\n");
printf("\nEmployee management system");
printf("\n1. Insert employee information");
printf("\n2. List all employee information");
printf("\n3. Edit employee information");
printf("\n4. Delete employee information");
printf("\n5. Exit");
printf("\n\n*****************************\n");
printf("\n\n Enter your choice: ");
scanf("%d", &choice);
int ch; while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
switch(choice)
{
case 1:
puts("Insert was chosen");
insert();
break;
case 2:
puts("List was chosen");
list();
break;
case 3:
puts("Edit was chosen");
edit();
break;
case 4:
puts("Delete was chosen");
del();
break;
case 5:
puts("Exit was chosen");
exit(1);
break;
default:
puts("Choice is incorrect!!");
break;
}
}
return 0;
}
void insert()
{
char next;
do
{
printf("********************************************************** \n");
printf("\nEnter the name of the employee: ");
fgets(e.name, sizeof(e.name), stdin);
printf("\nEnter the sex of the employee (M/m or F/f): ");
scanf("%6s",e.sex);
switch(*e.sex)
{
case 'M':
case 'm':
printf("\nMale.\n");
break;
case 'F':
case 'f':
printf("\nFemale.\n ");
break;
default:
printf("Unspecified Sex.");
}
printf("\nEnter the address of the employee: ");
fseek(stdin, 0, SEEK_END); // ADD THIS TO AVOID SKIP
fgets(e.adrs, sizeof(e.adrs), stdin); // this
printf("\nEnter designation of the employee: ");
fgets(e.dsgn, sizeof(e.dsgn), stdin); // this
printf("\nEnter age of the employee: ");
scanf("%d", &e.age);
printf("\nEnter basic salary of the employee: ");
scanf("%f", &e.slry);
printf("\nEnter the employee's ID: ");
scanf("%d", &e.empID);
fputs(e.name, fptr);
fputs(e.sex, fptr);
fputs(e.adrs, fptr);
fputs(e.dsgn, fptr);
fprintf(fptr, "%d \n%f \n%d \n", e.age, e.slry, e.empID);
// fwrite(&e,recsize,1,fptr);
int ch; while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
//fflush(stdin);//
printf("\nDo you want to input more? (y/n): ");
next = getche();
printf("\n");
}
while( tolower(next) != 'n' );
fclose(fptr);
}
void list ()
{
printf("-------------------------------");
printf("\nEmployee Details: \n---------------------------------\n");
rewind(fptr);///moves file to start of the file
while(fread(&e,recsize,1,fptr)==1)///read the file and fetch the record one record per fetch
{
printf("\n\n%s \t\t%6s \t%s \t%s \t%d \t%.2f \t%d",e.name, e.sex, e.adrs, e.dsgn, e.age, e.slry, e.empID);
}
getch();
/*printf("Name : %s\n",e.name);
printf("Address : %s\n",e.adrs);
printf("Sex : %c\n",e.sex);
printf("Designation : %s\n",e.dsgn);
printf("Age : %d\n",e.age);
printf("Salary : %.2f\n",e.slry);
printf("Employee-ID : %d\n",e.empID);*/
}
void edit ()
{
char next;
do
{
printf("Enter the employee's name to be edited: ");
scanf("%49[^\n]", empname);
rewind(fptr);
while(fread(&e, recsize, 1, fptr)==1)///fetch all records from file
{
if(strcmp(e.name,empname) == 0) ///if entered name matches with that in file
printf("\nEnter new name, sex, address, designation, age, salary and employee ID: ");
scanf("%s%c%s%s%d%f%d", e.name, e.sex, e.adrs, e.dsgn, &e.age, &e.slry, &e.empID);
fseek(fptr, -recsize, SEEK_CUR);/// move cursor 1 step back from current position
fwrite(&e, recsize,1,fptr); ///override the record
break;
}
printf("\nEdit another record(y/n)");
next = getche();
int ch; while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
}
while(next != 'n');
return ;
}
void del()
{
char next;
do
{
printf("\nEnter name of employee to delete: ");
scanf("%s",empname);
ftemp = fopen("Temp.dat","wb"); ///create a intermediate file for temporary storage
rewind(fptr); ///move record to starting of file
while(fread(&e,recsize,1,fptr) == 1) ///read all records from file
{
if(strcmp(e.name,empname) != 0) ///if the entered record match
{
fwrite(&e,recsize,1,ftemp); ///move all records except the one which is to be deleted to temp file
}
}
fclose(fptr);
fclose(ftemp);
remove("ems.txt"); ///remove original file
rename("Temp.dat","ems.txt"); ///rename temp file to original file name
fptr = fopen("ems.txt", "rb+");
printf("Delete another record(y/n)");
int ch; while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
next = getche();
}while( tolower(next) != 'n' );
fclose(fptr);
exit(0);
}
When you get the list of employees, you should use EOF like this.
fread(&e,recsize,1,fptr)!=EOF
I wish it's helpful for you.
Related
I am creating a module for club management system, but the function won't loop and save records. Sometimes it will loop but didn't save any record, sometimes won't loop but save the record. Can anyone fix my code?
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<conio.h>
#pragma warning(disable:4996)
// Structure of the employee
struct staff
{
char name[40];
int id[7];
int age[3];
char position[20];
};
struct staff s;
// size of the structure
long int recsize = sizeof(s);
FILE* fp, * ft;
void addrecord();
void deleterecord();
void displayrecord();
void modifyrecord();
void searchrecord();
void staffmenu();
char another;
//main menu
int menu()
{
int funsType;
fp = fopen("staff.dat", "rb+");
if (fp == NULL) {
fp = fopen("staff.dat", "wb+");
if (fp == NULL) {
printf("\nCannot open file...");
exit(1);
}
}
system("cls");
printf("1) Staff Information\n");
printf("2) Facility Information\n");
printf("3) Member Information\n");
printf("4) Booking Information\n");
printf("5) Facility Usage Information\n");
printf("Enter your requirement: ");
scanf("%d", &funsType);
switch (funsType) {
case 1: staffmenu(); break;
case 2: printf("2"); break;
case 3: printf("3"); break;
case 4: printf("4"); break;
case 5: printf("5"); break;
default: printf("Please choose the correct number");
getch();
menu();
}
return 0;
}
int main() {
menu();
staffmenu();
}
//show staff menu
void staffmenu() {
system("cls");
int choice;
printf("\n1. ADD RECORD\n");
printf("\n2. DELETE RECORD\n");
printf("\n3. DISPLAY RECORDS\n");
printf("\n4. MODIFY RECORD\n");
printf("\n5. SEARCH RECORD\n");
printf("\n6. EXIT\n");
printf("\nENTER YOUR CHOICE...\n");
fflush(stdin);
scanf("%d", &choice);
switch (choice) {
case 1:
addrecord();
break;
case 2:
deleterecord();
break;
case 3:
displayrecord();
break;
case 4:
modifyrecord();
break;
case 5:
searchrecord();
break;
case 6:
fclose(fp);
exit(0);
break;
default:
printf("\nINVALID CHOICE...\n");
getch();
staffmenu();
}
}
//add record to binary file
void addrecord()
{
system("cls");
another = 'y';
fseek(fp, 0, SEEK_END);
while (another == 'y') {
printf("\nEnter ID : ");
scanf("%s", &s.id);
printf("\nEnter Name : ");
scanf("%s", s.name);
printf("\nEnter Age : ");
scanf("%s", &s.age);
printf("\nEnter Position : ");
scanf("%s", s.position);
fwrite(&s, recsize, 1, fp);
printf("\nWant to add another record (Y/N) : ");
fflush(stdin);
scanf("%c", &another); //When I click n i will back to addrecord but the record didnt't save //It will loop but didn't save the record
another = getche();//read from keyboard
}
}
//delete record from binary file
void deleterecord()
{
system("cls");
char empname[50];
char another = 'y';
while (another == 'y') {
printf("Enter employee name to delete : ");
scanf("%s", empname);
ft = fopen("temp.dat", "wb");
rewind(fp);
while (fread(&s, recsize, 1, fp) == 1) {
if (strcmp(s.name, empname) != 0)
fwrite(&s, recsize, 1, ft);
}
fclose(fp);
fclose(ft);
remove("staff.dat");
rename("temp.dat", "staff.dat");
fp = fopen("staff.dat", "rb+");
printf("\nWant to delete another record (Y/N) :");
fflush(stdin);
another = getche(); // when I click n it will straight close the program but still no delete the record //It wont loop and save the record
}
}
//display all record from file
void displayrecord()
{
system("cls");
fp = fopen("staff.dat", "rb+");
if (fp == NULL) {
fp = fopen("staff.dat", "wb+");
if (fp == NULL) {
printf("\nCannot open file...");
exit(1);
}
}
rewind(fp);
printf("\n==========================================================");
printf("\nID\t\tNAME\t\tAGE\t\t\tPOSITION\n",s.id, s.name, s.age, s.position);
printf("==========================================================\n");
while (fread(&s, recsize, 1, fp) == 1)
printf("\n%s\t\t%s\t\t%s\t%s",s.id, s.name, s.age, s.position);
printf("\n\n\n\t");
printf("Press any key to back to menu......");
if (getch())
staffmenu();
}
//edit record from binary file
void modifyrecord()
{
system("cls");
char empname[50];
char another = 'y';
while (another == 'y') {
printf("\nEnter employee name to modify : ");
scanf("%s", empname);
rewind(fp);
while (fread(&s, recsize, 1, fp) == 1) {
if (strcmp(s.name, empname) == 0) {
printf("\nEnter new id:");
scanf("%s", &s.id);
printf("\nEnter new name :");
scanf("%s", s.name);
printf("\nEnter age :");
scanf("%s", &s.age);
printf("\nEnter new position :");
scanf("%s", &s.position);
fseek(fp, -recsize, SEEK_CUR);
fwrite(&s, recsize, 1, fp);
break;
}
}
// Ask for modifying another record
printf("\nWant to modify another"
" record (Y/N) :");
fflush(stdin);
another = getche();
}
}
//search record from binary file
// it can't continue search, it will trigger breakpoint
void searchrecord() {
system("cls");
int found = 0;
char namesearch[40];
char another = 'y';
while (another == 'y') {
printf("\n Enter Name to search : ");
scanf("%s", &namesearch);
if (fp == NULL) {
fp = fopen("staff.dat", "wb+");
if (fp == NULL) {
printf("\nCannot open file...");
exit(1);
}
}
rewind(fp);
while ((fread(&s, recsize, 1, fp) == 1))
{
if (strcmp(s.name, namesearch) == 0)
{
found = 1;
printf("\nRecord of Staff / Member of %s is\n", namesearch);
printf("===================================");
printf("\nID : %s", s.id);
printf("\nName : %s", s.name);
printf("\nAge : %s", s.age);
printf("\nPosition : %s", s.position);
}
}
if (found == 0)
printf("\nRecord Not Found\n");
fclose(fp);
printf("\nSearch another record(y/n) :");
fflush(stdin);
another = getche();
}
}
Hi i think the problem is that '\n' from the previous input is in the input stream. My solution would be not to read a char but a string.
// Structure of the employee
char another; -> char another[2];
//show staff menu
//void addrecord()
another = 'y'; -> sprintf(another, "y");
while (another == 'y') -> while (strcmp(another, "y") == 0)
scanf("%c", &another); another = getche(); -> scanf_s("%s", &another, (unsigned)sizeof(another));
these are only a few changes for void addrecord().
I think you would need to change everything with "another".
While debugging this code I get the following error: "conflicting types for 'fptr'." This shows at 3 lines with the same piece of code. I'll just identify one line: 158 in the void list() section. I'm no expert in files. I ask if you know what's the issue if you could please intervene? Thank you in advance!!
Header file:
struct employee
{
char name[50];
char sex[7];
char adrs[50];
char dsgn[25];
int age;
int empID[9];
float slry;
};
Problem:
printf("-------------------------------");
printf("\nEmployee Details: \n---------------------------------\n");
rewind(fptr);///moves file to start of the file
size_t fread(void *fptr, size_t size, size_t nmemb, FILE *fptr)///read the file and fetch the
record one record per fetch
Entire code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <stdbool.h>
#include <windows.h>
#include "struct.h"
void insert();
void list();
void edit();
void del();
void exit();
int tolower();
FILE * fptr, *ftemp;
struct employee e;
size_t recsize ;
char empname[50];
int main()
{
int choice;
fptr = fopen("ems.txt", "r+");
if (fptr == NULL)
{
printf("Can't find file! Attempting to create file... \n");
fptr = fopen("ems.txt","w+");
if(fptr == NULL)
{
printf("Can't create file. Exiting...");
exit(1);
}
}
//Explain the reason for this?
recsize = (long int) sizeof(e);
while(1)
{
printf("*******************************\n");
printf("\nEmployee management system");
printf("\n1. Insert employee information");
printf("\n2. List all employee information");
printf("\n3. Edit employee information");
printf("\n4. Delete employee information");
printf("\n5. Exit");
printf("\n\n*****************************\n");
printf("\n\n Enter your choice: ");
scanf("%d", &choice);
int ch; while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
switch(choice)
{
case 1:
puts("Insert was chosen");
insert();
break;
case 2:
puts("List was chosen");
list();
break;
case 3:
puts("Edit was chosen");
edit();
break;
case 4:
puts("Delete was chosen");
del();
break;
case 5:
puts("Exit was chosen");
exit(1);
break;
default:
puts("Choice is incorrect!!");
break;
}
}
return 0;
}
void insert()
{
char next;
do
{
printf("********************************************************** \n");
printf("\nEnter the name of the employee: ");
fgets(e.name, sizeof(e.name), stdin);
e.name[strcspn(e.name, "\n\r")] = '\0';
printf("\nEnter the sex of the employee (M/m or F/f): ");
scanf("%6s",e.sex);
switch(*e.sex)
{
case 'M':
case 'm':
printf("\nMale.\n");
break;
case 'F':
case 'f':
printf("\nFemale.\n ");
break;
default:
printf("Unspecified Sex.");
}
printf("\nEnter the address of the employee: ");
fseek(stdin, 0, SEEK_END); // ADD THIS TO AVOID SKIP
fgets(e.adrs, sizeof(e.adrs), stdin); // this
e.adrs[strcspn(e.adrs, "\n\r")] = '\0';
printf("\nEnter designation of the employee: ");
fgets(e.dsgn, sizeof(e.dsgn), stdin); // this
e.dsgn[strcspn(e.dsgn, "\n\r")] = '\0';
printf("\nEnter age of the employee: ");
scanf("%d", &e.age);
printf("\nEnter basic salary of the employee: ");
scanf("%f", &e.slry);
printf("\nEnter the employee's ID: ");
scanf("%8d", e.empID);
fprintf(fptr, "%s\n", e.name);
fprintf(fptr, "%6s\n", e.sex);
fprintf(fptr, "%s\n", e.adrs);
fprintf(fptr, "%s\n", e.dsgn);
fprintf(fptr, "%d \n%f \n%d \n", e.age, e.slry, e.empID[9]);
fwrite(&e,recsize,1,fptr);
int ch; while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
//fflush(stdin);//
printf("\nDo you want to input more? (y/n): ");
next = getche();
printf("\n");
}
while( tolower(next) != 'n' );
//fclose(fptr);
}
void list ()
{
printf("-------------------------------");
printf("\nEmployee Details: \n---------------------------------\n");
rewind(fptr);///moves file to start of the file
L158/// size_t fread(void *fptr, size_t size, size_t nmemb, FILE *fptr)///read the file and fetch the
record one record per fetch
{
printf("\n\n%s \t\t%6s \t%s \t%s \t%d \t%.2f \t%d",e.name, e.sex, e.adrs, e.dsgn, e.age,
e.slry, e.empID[9]);
return;
}
getch();
/*printf("Name : %s\n",e.name);
printf("Address : %s\n",e.adrs);
printf("Sex : %c\n",e.sex);
printf("Designation : %s\n",e.dsgn);
printf("Age : %d\n",e.age);
printf("Salary : %.2f\n",e.slry);
printf("Employee-ID : %d\n",e.empID);*/
}
void edit ()
{
char next;
do
{
printf("Enter the employee's name to be edited: ");
scanf("%49[^\n]", empname);
rewind(fptr);
size_t fread(void *fptr, size_t size, size_t nmemb, FILE *fptr)///fetch all records from file
{
if(strcmp(e.name,empname) == 0) ///if entered name matches with that in file
{
printf("\nEnter new name, sex, address, designation, age, salary and employee ID: ");
scanf("%s%c%s%s%d%f%d", e.name, e.sex, e.adrs, e.dsgn, &e.age, &e.slry, e.empID);
fseek(fptr, -recsize, SEEK_CUR);/// move cursor 1 step back from current position
fwrite(&e, recsize,1,fptr); ///override the record
break;
}
return ;}
printf("\nEdit another record(y/n)");
next = getche();
int ch; while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
}
while(next != 'n');
return ;
}
void del()
{
char next;
do
{
printf("\nEnter name of employee to delete: ");
scanf("%s",empname);
ftemp = fopen("Temp.dat","wb"); ///create a intermediate file for temporary storage
rewind(fptr); ///move record to starting of file
size_t fread(void *fptr, size_t size, size_t nmemb, FILE *fptr) ///read all records from file
{
if(strcmp(e.name,empname) != 0) ///if the entered record match
{
fwrite(&e,recsize,1,ftemp); ///move all records except the one which is to be deleted to temp file
}
return ;
}
fclose(fptr);
fclose(ftemp);
remove("ems.txt"); ///remove original file
rename("Temp.dat","ems.txt"); ///rename temp file to original file name
fptr = fopen("ems.txt", "rb+");
printf("Delete another record(y/n)");
int ch; while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
next = getche();
}while( tolower(next) != 'n' );
fclose(fptr);
exit(0);
}
While running this code I get an unwanted display: "the information for the salary is not displaying although I inputted the information" This problem most likely exist in line 131 in the insert section (). If I enter "900" for salary it displays "0.00." I also think it's related to the line of code just below it since the display of data changes back to normal however the data for the employee ID changes and prints the address. I ask if you know what's the issue if you could please intervene? Thank you in advance!!
struct here:
struct employee
{
char name[50];
char sex[7];
char adrs[50];
char dsgn[25];
int age;
int empID[9];
float slry;
};
Problem:
printf("\nEnter basic salary of the employee: ");
scanf("%f", &e.slry);
printf("\nEnter the employee's ID: ");
scanf("%8d", &e.empID[9]);
Entire code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <stdbool.h>
#include <windows.h>
#include "struct.h"
void insert();
void list();
void edit();
void del();
void exit();
int tolower();
FILE * fptr, *ftemp;
struct employee e;
size_t recsize;
char empname[50];
int main()
{
int choice;
fptr = fopen("ems.txt", "r+");
if (fptr == NULL)
{
printf("Can't find file! Attempting to create file... \n");
fptr = fopen("ems.txt","w+");
if(fptr == NULL)
{
printf("Can't create file. Exiting...");
exit(1);
}
}
//Explain the reason for this?
recsize = (long int) sizeof(e);
while(1)
{
printf("*******************************\n");
printf("\nEmployee management system");
printf("\n1. Insert employee information");
printf("\n2. List all employee information");
printf("\n3. Edit employee information");
printf("\n4. Delete employee information");
printf("\n5. Exit");
printf("\n\n*****************************\n");
printf("\n\n Enter your choice: ");
scanf("%d", &choice);
int ch; while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
switch(choice)
{
case 1:
puts("Insert was chosen");
insert();
break;
case 2:
puts("List was chosen");
list();
break;
case 3:
puts("Edit was chosen");
edit();
break;
case 4:
puts("Delete was chosen");
del();
break;
case 5:
puts("Exit was chosen");
exit(1);
break;
default:
puts("Choice is incorrect!!");
break;
}
}
return 0;
}
void insert()
{
char next;
do
{
printf("********************************************************** \n");
printf("\nEnter the name of the employee: ");
fgets(e.name, sizeof(e.name), stdin);
e.name[strcspn(e.name, "\n\r")] = '\0';
printf("\nEnter the sex of the employee (M/m or F/f): ");
scanf("%6s",e.sex);
switch(*e.sex)
{
case 'M':
case 'm':
break;
case 'F':
case 'f':
break;
default:
printf("Unspecified Sex.");
}
printf("\nEnter the address of the employee: ");
fseek(stdin, 0, SEEK_END); // ADD THIS TO AVOID SKIP
fgets(e.adrs, sizeof(e.adrs), stdin); // this
e.adrs[strcspn(e.adrs, "\n\r")] = '\0';
printf("\nEnter designation of the employee: ");
fgets(e.dsgn, sizeof(e.dsgn), stdin); // this
e.dsgn[strcspn(e.dsgn, "\n\r")] = '\0';
printf("\nEnter age of the employee: ");
scanf("%d", &e.age);
printf("\nEnter basic salary of the employee: ");
scanf("%f", &e.slry);
printf("\nEnter the employee's ID: ");
scanf("%8d", &e.empID[9]);
fwrite(&e,recsize,1,fptr);
int ch; while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
printf("\nDo you want to input more? (y/n): ");
next = getche();
printf("\n");
}
while( tolower(next) != 'n' );
//fclose(fptr);
}
void list ()
{
/*printf("-------------------------------");
printf("\nEmployee Details: \n---------------------------------\n");
rewind(fptr);///moves file to start of the file
size_t fread(void *fptr, size_t size, size_t nmemb, FILE *stream)///read the file and fetch the record one record per fetch
{
printf("\n\n%s \t\t%6s \t%s \t%s \t%d \t%.2f \t%d",e.name, e.sex, e.adrs, e.dsgn, e.age, e.slry, e.empID[9]);
return 1;
}
getch();*/
printf("-------------------------------");
printf("\nEmployee Details: \n---------------------------------\n");
rewind(fptr);
/*size_t fread(void *fptr, size_t size, size_t nmemb, FILE *stream)///read the file and fetch the record one record per fetch
{*/
printf("Name : %s\n",e.name);
printf("Address : %s\n",e.adrs);
printf("Sex : %7s\n",e.sex);
printf("Designation : %s\n",e.dsgn);
printf("Age : %d\n",e.age);
printf("Salary : %.2f\n",e.slry);
printf("Employee-ID : %d\n",e.empID[9]);
/*return 1;
}*/
}
void edit ()
{
char next;
do
{
printf("Enter the employee's name to be edited: ");
scanf("%49[^\n]", empname);
rewind(fptr);
size_t fread(void *fptr, size_t size, size_t nmemb, FILE *stream)///fetch all records from file
{
if(strcmp(e.name,empname) == 0) ///if entered name matches with that in file
{
printf("\nEnter new name, sex, address, designation, age, salary and employee ID: ");
scanf("%s%c%s%s%d%f%d", e.name, e.sex, e.adrs, e.dsgn, &e.age, &e.slry, e.empID);
fseek(fptr, -recsize, SEEK_CUR);/// move cursor 1 step back from current position
fwrite(&e, recsize,1,fptr); ///override the record
}
return 1;}
printf("\nEdit another record(y/n)");
next = getche();
int ch; while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
}
while(next != 'n');
return ;
}
void del()
{
char next;
do
{
printf("\nEnter name of employee to delete: ");
scanf("%s",empname);
ftemp = fopen("Temp.dat","wb"); ///create a intermediate file for temporary storage
rewind(fptr); ///move record to starting of file
size_t fread(void *fptr, size_t size, size_t nmemb, FILE *stream) ///read all records from file
{
if(strcmp(e.name,empname) != 0) ///if the entered record match
{
fwrite(&e,recsize,1,ftemp); ///move all records except the one which is to be deleted to temp file
}
return 1;
}
fclose(fptr);
fclose(ftemp);
remove("ems.txt"); ///remove original file
rename("Temp.dat","ems.txt"); ///rename temp file to original file name
fptr = fopen("ems.txt", "rb+");
printf("Delete another record(y/n)");
int ch; while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
next = getche();
}while( tolower(next) != 'n' );
fclose(fptr);
exit(0);
}
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");
}
}
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);
// ^^^^^
}
}
I suspect my program is not working properly. The output is not what I would expect. When I run this program the first time, the result is different than if we repeat the action without closing the program, for example add new record..
Here is the source code I have as of now:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct emp{
char name[40];
int age;
float bs;
};
main()
{
FILE *fp, *ft;
struct emp e;
char choice;
char another;
char empname[40];
fp = fopen("emp.dat","rb+");
if(fp == NULL)
{
fp = fopen("emp.dat", "wb+");
if(fp == NULL)
{
printf("File cannot be opened\n");
system("pause");
exit(0);
}
}
while(1)
{
system("cls");
printf("1. Add New Record\n");
printf("2. Modify to Record\n");
printf("3. Delete to Record\n");
printf("4. Display Records\n");
printf("0. Exit\n");
printf("Enter your choice: \n");
fflush(stdin);
choice = getche();
printf("\n");
switch(choice)
{
case '1':
another = 'Y';
fseek(fp,0,SEEK_END);
while(another == 'Y')
{
printf("Enter name, age and basic salary: \n");
scanf("%s%d%f", e.name, &e.age, &e.bs);
fwrite(&e,sizeof(e),1,fp);
printf("Add more records (Y/N)\n");
fflush(stdin);
scanf("%c", &another);
}
break;
case '2':
printf("Enter name of employee to modify record!\n");
scanf("%s", empname);
rewind(fp);
while(fread(&e,sizeof(e),1,fp) == 1)
{
while(strcmp(e.name,empname) == 0)
{
fseek(fp,-1,SEEK_CUR);
printf("Enter new name, age, basic salary!\n");
scanf("%s %d %f", e.name, &e.age, &e.bs);
fwrite(&e,sizeof(e),1,fp);
}
}
break;
case '3':
another = 'Y';
while(another == 'Y')
{
rewind(fp);
ft = fopen("temp.dat", "wb+");
printf("Enter name of employee to delete record!\n");
scanf("%s", &empname);
while(fread(&e,sizeof(e),1,fp) == 1)
{
while(strcmp(e.name,empname) != 0)
{
fseek(fp,-1,SEEK_CUR);
fwrite(&e,sizeof(e),1,ft);
}
fclose(fp);
fclose(ft);
remove("emp.dat");
rename("temp.dat","emp.dat");
fp = fopen("emp.dat","wb+");
}
printf("Do you want to delete more record/s (Y/N)!\n");
fflush(stdin);
another = getche();
}
break;
case '4':
rewind(fp);
while(fread(&e, sizeof(e), 1, fp) == 1)
printf("%s %d %0.2f\n", e.name, e.age, e.bs);
break;
case '0':
exit(0);
break;
default:
printf("Enter correct choice!\n");
break;
}
fclose(fp);
system("pause");
}
}
Here is my program running, and with an example output: