K
kimimaro
Thank you for your help I think I know what the problem is. Firstly the
add_record cannot read the record.txt or something if the record.txt (in
which I used it to store the details of each record) is present in the
same directory. When it prompt
enter your selection from the menu, it will just stuck there so I cant
even create a new entry and thus I cant check the modify function in my
program. If it's not present, it will create a record.txt once I've add
everything in it when I view
in the record.txt file, these shows up
I dont know if it's correct or it should be (for example) :
Below are the source codes :
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
char Name[50];
char Department [4] [50] = {"Accounting", "Administration", "Management"
,
"Others"};
int ID[50];
char Gender [2] [50] = {"Male", "Female"} ;
char *stars="********================*********";
char *lines="---------------------------------";
char *empty=" ";
int txt_inserted;
/*Functions*/
void modify_record();
void add_record();
void search_record();
//void list_record();
void delete_record();
//void exit();
void menu();
/* MAIN */
main(){
clrscr();
menu();
getch();
return 0;
}
/* FUNCTION: MENU */
void menu(){
int selection;
FILE *start;
if((start = fopen("record.txt", "a+")) == NULL)
printf(" File record.txt is not found! Previous saved employee records
did
not loaded\n\n");
else {
fscanf(start, "%i %*c %c %*c, &ID", &Name, &Gender, &Department);
printf(" Values Uploaded: %i %*c %c %*c", &ID, &Name, &Gender,
&Department);
fclose(start);}
gotoxy(15,1);
puts("\n -------* [Welcome To Net Communication Human Resource]
*-------");
printf("\t%s\n",stars);
puts("\t#\t1.Staff Biodata Maintenance\t#");
puts("\t#\t2.Search Employee Records via Emplyee ID\t#");
puts("\t#\t3.Display all employee by department\t#");
puts("\t#\t4.Employee Record deletion\t#");
puts("\t#\t5.Exit\t#");
printf("\t%s\n",stars);
printf("\n Enter Your Selection: ");
fflush(stdin);
scanf("%d", &selection);
if (selection == 1) add_record();
else if (selection == 2) search_record();
//else if (selection == 3) list_record();
else if (selection == 4) delete_record();
//else if (selection == 5) exit();
else{ printf(" Please only select from the above menu"); getch();
clrscr(); menu();}
}
/*Function 1a*/
void modify_record(){
char Name[50];
int Target, ID, selection, Found=0;
FILE *rec, *temp;
temp=fopen("temp.txt", "w+");
if((rec=fopen("record.txt", "r+")) == NULL)
printf("\nerror: file not found.\n");
else {
printf(" ENTER the employee ID to be modified: ");
scanf("%d", &Target);
while(!feof(rec)){
fscanf(rec, "%i %*c %c %*c", &ID, &Name, &Gender, &Department);
if(feof(rec))
break;
else if(Target==ID)
fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department);
else
{
Found=1;
printf("\n Record Found! Please re-enter details for the
employee ::%s::", Name);
/*Modify employee details*/
gotoxy(1,15);
printf("\n Employee details edit ");
do
{
gotoxy(1, 22);
printf(" Name : ");
gotoxy(10, 22);
fflush(stdin);
scanf("%s",&Name);
printf(" Gender : ");
fflush(stdin);
scanf("%s",&Gender);
printf("Please enter the department for this employee, [0]
Administration [1] Management [2] Accounting [3] Others", empty, empty,
empty, empty);
fflush(stdin);
scanf("%d",&selection);
if(selection==0 ){
printf("\n Your selection is %s\n\n%s%s\n ",
Department[selection], empty, empty);
fprintf(temp, "%i %*c %c %*c", ID, Name, Gender,
Department[selection]);
}
else if(selection==1){
printf("\n Your selection is %s\n\n%s%s\n ",
Department[selection], empty, empty);
fprintf(temp, "%i %*c %c %*c", ID, Name, Gender,
Department[selection]);
}
else if(selection==2){
printf("\n Your selection is %s\n\n%s%s\n ",
Department[selection], empty, empty);
fprintf(temp, "%i %*c %c %*c", ID, Name, Gender,
Department[selection]);
}
else if(selection==3){
printf("\n Your selection is %s\n\n%s%s\n ",
Department[selection], empty, empty);
fprintf(temp, "%i %*c %c %*c", ID, Name, Gender,
Department[selection]);
}
else
printf("\n\n INVALID CHOICE! PLEASE Re-Enter.\a");
} while(selection < 0 || selection > 3);
}
}
if(!Found)
printf("\n RECORD CANNOT BE FOUND!\n");
add_record();
}
fclose(rec);
fclose(temp);
remove("record.txt");
rename("temp.txt", "record.txt");
getch();
clrscr();
menu();
}
/*Function 1b adding employee*/
void add_record(){
int ID;
int selection;
FILE *rec, *temp;
if ((rec=fopen("record.txt","a+"))==NULL)
ID = 1;
else
{
do{
fscanf(rec, "%i %*c %c %*c", &ID, &Name, &Gender, &Department);
}while(!feof(rec));
ID += 1;
}
do {
gotoxy(2, 15);
printf("\n\n 0) %s\n 1) %s\n 2) %s\n 3) %s\n\n [Enter Department for
this new employee]: ", Department[0], Department[1], Department[2],
Department[3]);
fflush(stdin);
scanf("%d", &selection);
if((selection != 0) && (selection != 1) && (selection !=2) &&
(selection !=3)) {gotoxy(1, 24); printf(" >>invalid Department<<
Retry!\a");}
} while((selection != 0) && (selection != 1) && (selection !=2) &&
(selection !=3));
clrscr();
printf("The new ID for this employee: %04i", ID);
printf("\n Employee Name : \n");
fflush(stdin);
fgets(Name, 49, stdin);
printf("\n Employee Gender: \n");
scanf("%c", &Gender);
printf("\nThe New Employee Record have successfully Saved!");
fprintf(rec, "%i %[^/]%*c %c %c", ID, Name, Gender,
Department);
printf("\n Employee Status Stored!");
fclose(rec);
}
add_record cannot read the record.txt or something if the record.txt (in
which I used it to store the details of each record) is present in the
same directory. When it prompt
enter your selection from the menu, it will just stuck there so I cant
even create a new entry and thus I cant check the modify function in my
program. If it's not present, it will create a record.txt once I've add
everything in it when I view
in the record.txt file, these shows up
4198977 %[^/]%*c %c %c
I dont know if it's correct or it should be (for example) :
4198977(ID), Rebecca( Name), F(Gender), Accounting(Department)
Below are the source codes :
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
char Name[50];
char Department [4] [50] = {"Accounting", "Administration", "Management"
,
"Others"};
int ID[50];
char Gender [2] [50] = {"Male", "Female"} ;
char *stars="********================*********";
char *lines="---------------------------------";
char *empty=" ";
int txt_inserted;
/*Functions*/
void modify_record();
void add_record();
void search_record();
//void list_record();
void delete_record();
//void exit();
void menu();
/* MAIN */
main(){
clrscr();
menu();
getch();
return 0;
}
/* FUNCTION: MENU */
void menu(){
int selection;
FILE *start;
if((start = fopen("record.txt", "a+")) == NULL)
printf(" File record.txt is not found! Previous saved employee records
did
not loaded\n\n");
else {
fscanf(start, "%i %*c %c %*c, &ID", &Name, &Gender, &Department);
printf(" Values Uploaded: %i %*c %c %*c", &ID, &Name, &Gender,
&Department);
fclose(start);}
gotoxy(15,1);
puts("\n -------* [Welcome To Net Communication Human Resource]
*-------");
printf("\t%s\n",stars);
puts("\t#\t1.Staff Biodata Maintenance\t#");
puts("\t#\t2.Search Employee Records via Emplyee ID\t#");
puts("\t#\t3.Display all employee by department\t#");
puts("\t#\t4.Employee Record deletion\t#");
puts("\t#\t5.Exit\t#");
printf("\t%s\n",stars);
printf("\n Enter Your Selection: ");
fflush(stdin);
scanf("%d", &selection);
if (selection == 1) add_record();
else if (selection == 2) search_record();
//else if (selection == 3) list_record();
else if (selection == 4) delete_record();
//else if (selection == 5) exit();
else{ printf(" Please only select from the above menu"); getch();
clrscr(); menu();}
}
/*Function 1a*/
void modify_record(){
char Name[50];
int Target, ID, selection, Found=0;
FILE *rec, *temp;
temp=fopen("temp.txt", "w+");
if((rec=fopen("record.txt", "r+")) == NULL)
printf("\nerror: file not found.\n");
else {
printf(" ENTER the employee ID to be modified: ");
scanf("%d", &Target);
while(!feof(rec)){
fscanf(rec, "%i %*c %c %*c", &ID, &Name, &Gender, &Department);
if(feof(rec))
break;
else if(Target==ID)
fprintf(temp, "%i %*c %c %*c", ID, Name, Gender, Department);
else
{
Found=1;
printf("\n Record Found! Please re-enter details for the
employee ::%s::", Name);
/*Modify employee details*/
gotoxy(1,15);
printf("\n Employee details edit ");
do
{
gotoxy(1, 22);
printf(" Name : ");
gotoxy(10, 22);
fflush(stdin);
scanf("%s",&Name);
printf(" Gender : ");
fflush(stdin);
scanf("%s",&Gender);
printf("Please enter the department for this employee, [0]
Administration [1] Management [2] Accounting [3] Others", empty, empty,
empty, empty);
fflush(stdin);
scanf("%d",&selection);
if(selection==0 ){
printf("\n Your selection is %s\n\n%s%s\n ",
Department[selection], empty, empty);
fprintf(temp, "%i %*c %c %*c", ID, Name, Gender,
Department[selection]);
}
else if(selection==1){
printf("\n Your selection is %s\n\n%s%s\n ",
Department[selection], empty, empty);
fprintf(temp, "%i %*c %c %*c", ID, Name, Gender,
Department[selection]);
}
else if(selection==2){
printf("\n Your selection is %s\n\n%s%s\n ",
Department[selection], empty, empty);
fprintf(temp, "%i %*c %c %*c", ID, Name, Gender,
Department[selection]);
}
else if(selection==3){
printf("\n Your selection is %s\n\n%s%s\n ",
Department[selection], empty, empty);
fprintf(temp, "%i %*c %c %*c", ID, Name, Gender,
Department[selection]);
}
else
printf("\n\n INVALID CHOICE! PLEASE Re-Enter.\a");
} while(selection < 0 || selection > 3);
}
}
if(!Found)
printf("\n RECORD CANNOT BE FOUND!\n");
add_record();
}
fclose(rec);
fclose(temp);
remove("record.txt");
rename("temp.txt", "record.txt");
getch();
clrscr();
menu();
}
/*Function 1b adding employee*/
void add_record(){
int ID;
int selection;
FILE *rec, *temp;
if ((rec=fopen("record.txt","a+"))==NULL)
ID = 1;
else
{
do{
fscanf(rec, "%i %*c %c %*c", &ID, &Name, &Gender, &Department);
}while(!feof(rec));
ID += 1;
}
do {
gotoxy(2, 15);
printf("\n\n 0) %s\n 1) %s\n 2) %s\n 3) %s\n\n [Enter Department for
this new employee]: ", Department[0], Department[1], Department[2],
Department[3]);
fflush(stdin);
scanf("%d", &selection);
if((selection != 0) && (selection != 1) && (selection !=2) &&
(selection !=3)) {gotoxy(1, 24); printf(" >>invalid Department<<
Retry!\a");}
} while((selection != 0) && (selection != 1) && (selection !=2) &&
(selection !=3));
clrscr();
printf("The new ID for this employee: %04i", ID);
printf("\n Employee Name : \n");
fflush(stdin);
fgets(Name, 49, stdin);
printf("\n Employee Gender: \n");
scanf("%c", &Gender);
printf("\nThe New Employee Record have successfully Saved!");
fprintf(rec, "%i %[^/]%*c %c %c", ID, Name, Gender,
Department);
printf("\n Employee Status Stored!");
fclose(rec);
}