📄 工资管理.cpp
字号:
#include <stdio.h>
#include <conio.h>
#include <string.h>
/* structure that represents an employee record */
struct EMP {
char name[25];
float salary;
int deleted;
};
void main() {
EMP e; //Employee object for data storing & manipulation
strcpy(e.name,"NULL"); // Default values are
e.salary = 0; // assigned
e.deleted = 0;
while(1) {
char c; // User command is recieved in this variable
printf("\nDB >"); // Command prompt
c = getch(); // Getting user input. Type 'h' for possible values
if(c == 'n') { //New data is recieved int e
printf("\nName: "); // ( but it is not saved into a file)
scanf("%s",e.name);
printf("Salary: ");
scanf("%f",&e.salary);
e.deleted = 0;
}else if(c == 's') { //Saves current employee to a file
if((strcmp(e.name,"NULL")) == 0) { // Checks if user entered new data
printf("\nEnter employee data before trying to save");
continue;
}
/* If new data was entered a file ,EMP.dat,
is opened in append mode & employee record is
written to it */
FILE* file;
if((file = fopen("EMP.DAT","a"))==NULL) {
printf("\nFailed to save data");
continue;
}
fwrite((char*)&e,sizeof(e),1,file);
fclose(file);
printf("\nFile saved");
}else if(c == 'l') { // If user typed 'l' records in EMP.DAT is read int e in a loop
// & displayed to the user
FILE* file;
if((file = fopen("EMP.DAT","r")) == NULL) {
printf("\nFailed to read file");
continue;
}
long curpos, length; // Variables for finding file size
curpos = ftell(file); // Current position of filepointer is stored in curpos
fseek(file, 0L, SEEK_END);// File pointer is moved 0 bytes from file end
// ie,file pointer is at the file's last byte
length = ftell(file); // Now current position is file's length
fseek(file, curpos, SEEK_SET); // File pointer is restored to file's begining
long num_recs = length / sizeof(e);// Length of each record in the file
for(long l = 0;l<num_recs;l++) { // Recursivley retrieves & prints each record in file
fread((char*)&e,sizeof(e),1,file);
printf("\nName: %s",e.name);
printf("\nSalary: %.2f",e.salary);
}
fclose(file);
printf("\n\n\tRecords: %d",num_recs);
}else if(c == 'h') { // Prints help
printf("\nn - New\ns - Save\nl - List\nq - Quit");
}else if(c == 'q') { // Exists while loop & program
break;
}else if(c == 'f') { // Searches the database for an employee based on employee name
// entered by user
FILE* file;
if((file = fopen("EMP.DAT","r")) == NULL) { //Checks to see if file is accesible
printf("Failed to open file");
continue;
}
//Gets employee name to perform search
printf("Enter employee name: ");
char f[25];
scanf("%s",f);
//Finds file size
long curpos, length;
curpos = ftell(file);
fseek(file, 0L, SEEK_END);
length = ftell(file);
fseek(file, curpos, SEEK_SET);
long num_recs = length / sizeof(e);
int found = 0; // To check if record was found or not
for(long l = 0;l<num_recs;l++) {
// Recursivly checks each name if found prints details
// & sets found to 1
fread((char*)&e,sizeof(e),1,file);
if((strcmp(e.name,f))==0) {
printf("\nName: %s",e.name);
printf("Salary: %f",e.salary);
found = 1;
}
}
if(found == 0)
printf("Record not found.");
}else if(c == 'd') {// Deletes an employee
FILE* file;
FILE* temp_file;
if((file = fopen("EMP.DAT","r")) == NULL) {
printf("Failed to load data");
continue;
}
if((temp_file = fopen("EMP_TEMP.DAT","w")) == NULL) {
printf("Failed to create temp file");
continue;
}
printf("Enter employee name: ");
char f[25];
scanf("%s",f);
long curpos, length;
curpos = ftell(file);
fseek(file, 0L, SEEK_END);
length = ftell(file);
fseek(file, curpos, SEEK_SET);
long num_recs = length / sizeof(e);
int found = 0;
for(long l = 0;l<num_recs;l++) {
fread((char*)&e,sizeof(e),1,file);
if((strcmp(e.name,f))==0) {
e.deleted = 1; // If record found sets it deleted flag to true
found = 1;
}
fwrite((char*)&e,sizeof(e),1,temp_file);
// Write all records read to a temporory file
}
fclose(file);
fclose(temp_file);
if(found == 0) {
printf("Record not found.");
unlink("EMP_TEMP.DAT");//If record was not found deletes temp file
//& continues main command loop
continue;
}
//If record was found rewrites all records from temp file to EMP.DAT
//where deleted flag is 0
//& deletes the tem file in the end
file = fopen("EMP.DAT","w");
temp_file = fopen("EMP_TEMP.DAT","r");
curpos = ftell(temp_file);
fseek(temp_file, 0L, SEEK_END);
length = ftell(temp_file);
fseek(temp_file, curpos, SEEK_SET);
num_recs = length / sizeof(e);
for(l = 0;l<num_recs;l++) {
fread((char*)&e,sizeof(e),1,temp_file);
if(e.deleted == 0) {
fwrite((char*)&e,sizeof(e),1,file);
}
}
fclose(file);
fclose(temp_file);
printf("Record deleted");
unlink("EMP_TEMP.DAT");
}else if(c == 'e') { //Edits a record
FILE* file;
if((file = fopen("EMP.DAT","r")) == NULL) {
printf("Failed to open file");
continue;
}
printf("Enter employee name: ");
char f[25];
scanf("%s",f);
long curpos, length;
curpos = ftell(file);
fseek(file, 0L, SEEK_END);
length = ftell(file);
fseek(file, curpos, SEEK_SET);
long num_recs = length / sizeof(e);
int found = 0;
//Variables to store new name & salary
char new_name[25];
float new_sal;
EMP new_emp;
long write_at;//Position of record in to edit
for(long l = 0;l<num_recs;l++) {
fread((char*)&e,sizeof(e),1,file);
if((strcmp(e.name,f))==0) {//If record exists in file get new values
printf("Enter new name: ");
scanf("%s",new_name);
printf("Enter new salary: ");
scanf("%f",&new_sal);
//Copies values to a new EMP object
strcpy(new_emp.name,new_name);
new_emp.salary = new_sal;
new_emp.deleted = 0;
found = 1;
write_at = l; // Position to write new record
break;
}
}
fclose(file);
if(found == 1) {
//Calculates byte posiotn to write
write_at = write_at * sizeof(new_emp);
//Mode r+b opens a file to be writable & readable in binary
if((file = fopen("EMP.DAT","r+b")) == NULL) {
printf("Editing failed");
continue;
}
fseek(file,write_at,SEEK_SET);
fwrite((char*)&new_emp,sizeof(new_emp),1,file);
fclose(file);
printf("Record edited");
}else {
printf("Record not found");
}
}else {
printf("Type 'h' for valid commands ");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -