Нужно написать программу которая сохраняет данные dat.file - C (СИ)
Формулировка задачи:
Нужно написать программу которая сохраняет данные dat.file
Почему не создает "dat.file"! где ошибка?
есть идеи?
Program Descripption: Create a program to manage a employee database (on a single data file).
The program will display a menu in a loop (accepting 1:5 as values) which allows the user to:
1. Add Employee
2. Delete Employee
3. List Employees
4. Compact Records
5. Exit*/
#include <stdio.h>
#include <string.h>
#define FIRST_NAME_LENGTH 31
#define SECOND_NAME_LENGTH 51
#define NUMBER_LENGTH 21
#define ADDRESS 26
#define CODE 10
#define DURATION 2
#define TRUE 1
#define FALSE 0
/* Structure defining a name */
struct Name
{
char firstname[FIRST_NAME_LENGTH];
char secondname[SECOND_NAME_LENGTH];
char address[ADDRESS];
char department_code[CODE];
char duration[DURATION];
};
/* Structure defining a phone record */
struct Employee_number
{
struct Name name;
char Employeenumber[NUMBER_LENGTH];
};
/* Function prototypes */
struct Employee_number read_phonerecord(); /* Read a name and number from the keyboard */
struct Name read_name(); /* Read a name from the keyboard */
void show_record(struct Employee_number record); /* Output name and number from a phone record */
struct Employee_number find_numbers(struct Name name); /* Find numbers corresponding to a given name */
void add_record(); /* Add a new name and number */
void delete_records(struct Name name); /* Delete records for a given name */
void list_records(); /* List all the records in the file */
void show_operations(); /* Displays operations supported by the program */
int equals(struct Name name1, struct Name name2); /* Compare two names for equality */
/* Global variables */
FILE *pFile = NULL; /* File pointer */
char *employee = "C:\\employee.dat"; /* Name of the file holding the records */
char answer = 'n'; /* Stores input responses */
void main()
{
show_operations(); /* Display the available operations */
for(;
{
printf("\nEnter a number to select an operation: ");
scanf(" %c", &answer);
switch(answer)
{
case '1': /* Add a new name and number record */
add_record();
break;
case '2': /* Delete records for a given name */
delete_records(read_name());
break;
case '3': /* List all the name/number records */
list_records();
break;
case '4': /* Find the numbers for a given name */
find_numbers(read_name());
break;
case '5': /* Quit the program */
return;
default:
printf("\nInvalid selection try again.");
show_operations();
break;
}
}
}
/* Reads a name and number from the keyboard and creates a PhoneRecord structure */
struct Employee_number read_phonerecord()
{
struct Employee_number record;
record.name = read_name();
printf("Enter the number: ");
scanf(" %[ 0123456789]",record.Employeenumber); /* Read the number - including spaces */
return record;
}
/* Outputs the name and number from a phone record */
void show_record(struct Employee_number record)
{
printf("\n%s %s %s %s %s %s ", record.name.firstname,record.name.secondname, record.Employeenumber,record.name.address, record.name.department_code,record.name.duration);
}
/* Add a new name and number */
void add_record()
{
struct Employee_number record;
if((pFile = fopen(employee, "a+")) == NULL) /* Open/create file to be written in append mode */
{
printf("Error opening %s for writing. Program terminated.", employee);
}
record = read_phonerecord(); /* Read the name and number */
fwrite(&record, sizeof record, 1, pFile);
fclose(pFile); /* Close file */
printf("\nNew record added.");
}
/* Read a name from the keyboard and create a Name structure for it */
struct Name read_name()
{
struct Name name;
printf("Enter a first name: ");
scanf(" %s", name.firstname);
printf("Enter a second name: ");
scanf(" %s", name.secondname);
printf("Enter a address: ");
scanf("%s",name.address);
return name;
}
/* Delete records for a given name */
/* To delete one or more records we can copy
the contents of the existing file to a new
file, omitting the records that are to be
deleted. We can then delete the old file and
rename the new file to have the old file
name.
*/
void delete_records(struct Name name)
{
FILE *pNewFile = NULL;
char *pnewfilename = NULL;
struct Employee_number record;
if((pFile = fopen(employee, "r")) == NULL) /* Open current file to read it */
{
printf("Error opening %s for reading. Program terminated.", employee);
}
pnewfilename = tmpnam(NULL); /* Create temporary file name */
if((pNewFile = fopen(pnewfilename, "w")) == NULL) /* Open temporary file to write it */
{
printf("Error opening %s for writing. Program terminated.", pnewfilename);
fclose(pFile);
}
/* Copy existing file contents to temporary file, omitting deleted records */
for(;
{
fread(&record, sizeof record, 1, pFile); /* Read a record */
if(pFile) /* End of file read ? */
break; /* Yes-quit copy loop */
if(equals(name, record.name)) /* Is the record this name ? */
{
printf("\nFound a record:"); /* Ys, so it's a delete candidate */
show_record(record);
printf("\nDo you really want to delete it(y or n)? ");
scanf(" %c", &answer);
if((answer) == 'y') /* If it's to be deleted */
continue; /* Skip the copying */
}
fwrite(&record, sizeof record, 1, pNewFile); /* copy current record */
}
fclose(pFile);
fclose(pNewFile);
if((pNewFile = fopen(pnewfilename, "r")) == NULL) /* Open temporary file to read it */
{
printf("Error opening %s for reading. Program terminated.", pnewfilename);
}
if((pFile = fopen(employee, "w"))==NULL) /* Open original file to write it */
{
printf("Error opening %s for writing. Program terminated.", employee);
}
/* Copy contents of new temporary file back to old file */
/* This overwrites the original contents because the mode is "w" */
for(;
{
fread(&record, sizeof record, 1, pNewFile); /* Read temporary file */
if(pNewFile) /* If we read EOF */
break; /* We are done */
fwrite(&record, sizeof record, 1, pFile); /* Write record to original file */
}
fclose(pFile); /* Close the original file */
fclose(pNewFile); /* Close the temporary file */
remove(pnewfilename); /* Delete the temporary file */
printf("Delete complete.");
}
/* List all the records in the file */
void list_records()
{
struct Employee_number record;
int file_empty = TRUE; /* File empty flag */
if((pFile = fopen(employee, "r")) == NULL) /* Open the file to read it */
{
printf("Error opening %s for reading. Program terminated.", employee);
}
/* List the file contents */
for(;
{
fread(&record, sizeof record, 1, pFile);
if(pFile)
break;
file_empty = FALSE; /* We got a record so set empty flag false */
show_record(record); /* output the record */
}
fclose(pFile); /* Close the file */
/* Check whether there were any records */
if(file_empty)
printf("The file contains no records.\n");
else
printf("\n");
}
/* Displays the operations that are supported by the program */
void show_operations()
{
printf("The operations available are:"
"\n1: Add Employee."
"\n2: Delete Employee."
"\n3: List Employee."
"\n4: Compact Records."
"\n5: Exit.");
}
/* Find numbers corresponding to a given name */
struct Employee_number find_numbers(struct Name name)
{
struct Employee_number record;
int name_found = FALSE; /* Name found flag */
if((pFile = fopen(employee, "r")) == NULL) /* Open the file to read it */
{
printf("Error opening %s for reading. Program terminated.", employee);
}
/* Search the records read from the file */
for(;
{
fread(&record, sizeof record, 1, pFile); /* Read a record */
if(pFile)
break;
if(equals(name,record.name)) /* Is it the name requested? */
{
if(!name_found) /* Is this the first time we found it? */
{
name_found = TRUE; /* Yes so set flag to true */
printf("The numbers for this name are:"); /* Output initial message */
}
printf("\n%s",record.Employeenumber); /* Output the number */
}
}
fclose(pFile); /* Close the file */
/* Check for name not found */
if(!name_found)
printf("The name was not found.\n");
else
printf("\n");
}
/* Compare two names for equality */
int equals(struct Name name1, struct Name name2)
{
return (strcmp(name1.firstname, name2.firstname)==0) && (strcmp(name1.secondname, name2.secondname)==0);
}
Решение задачи: «Нужно написать программу которая сохраняет данные dat.file»
textual
Листинг программы
char *employee = "C:\\employee.dat"; /* Name of the file holding the records */
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д