Introduction
'C' is a totally function-based programming language. Creating a database structure and adding records, we have written the code in the main() function. The same code can be written in a separate function, which can be called by the main() function, or any other function for that matter.
The following program is an illustration of a function call to add records into a database file from an array of structures.
Source Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct employee {
int employee_code;
char employee_name[30];
char designation[20];
double salary;
};
void addrecords() {
FILE *file_pointer;
struct employee emp[10];
int i, no_of_records;
if ((file_pointer = fopen("emp1.dat", "a")) == NULL) {
fprintf(stderr, "Cannot open emp1.dat file to write records, terminating.\n");
exit(1);
}
while (1) {
printf("Enter the number of records to write <1 to 10>: ");
scanf("%d", &no_of_records);
fflush(stdin);
if (no_of_records > 0 && no_of_records < 11) {
break;
} else {
printf("Incorrect number of records, please correct.\n");
}
}
for (i = 0; i < no_of_records; i++) {
printf("\nEnter the employee code: ");
scanf("%d", &emp[i].employee_code);
fflush(stdin);
printf("Enter the employee name: ");
fgets(emp[i].employee_name, 30, stdin);
emp[i].employee_name[strcspn(emp[i].employee_name, "\n")] = 0; // Remove newline
printf("Enter the designation: ");
scanf("%s", emp[i].designation);
printf("Enter the salary: ");
scanf("%lf", &emp[i].salary); // Corrected format specifier for double
}
fwrite(emp, sizeof(struct employee), no_of_records, file_pointer);
fclose(file_pointer);
}
int main() {
// clrscr(); // Uncomment if using a specific compiler that supports clrscr
printf("Program to invoke a function to add records to a data file\n");
addrecords();
// getch(); // Uncomment if using a specific compiler that requires this
return 0;
}
Output
In the above program, we have only used one write () statement to write the entire array of structures.it is important to note, that in the argument for the number of records to be written, we have not used 1 but have used the variable no_of_records which contains the actual number of records that the user wants to write. An array of structures is written in a file in this manner only.
Developing a function to display record(s)
In a manner very similar to reading one record at a time, we can read a number of records from the file. To achieve this, we can use either one structure and read the records into it one by one or define an array of structures and read a block of records and do the necessary processing.
The first method of reading one record by one record is a safe method because the number of records in a file is not fixed, and generally not known in advance. Therefore, if we define an array of structures, it may be too big to store a lesser number of records or too small to store all the records.
Very rarely it is possible that the number of records in the file and the size of the array of pointer will be the same as they would be constant conditions and deletion to a data file.
The following program illustrates reading a number of records from a file into an array of structures.
#include<stdio.h>
#include<conio.h>
struct employee {
int employee_code;
char employee_name[30];
char designation[20];
float salary;
};
void disprecords();
void main() {
printf("Program to call function to read records into an array of structure\n");
disprecords();
}
void disprecords() {
FILE *file_pointer;
struct employee emp[10];
struct employee e;
int i, no_of_records = 0;
if ((file_pointer = fopen("employee.dat", "r")) == NULL) {
fprintf(stderr, "Cannot open employee.dat file for reading records, terminating\n");
exit(1);
}
while (fread(&e, sizeof(struct employee), 1, file_pointer) == 1)
no_of_records++;
if (no_of_records > 10) {
printf("Maximum number of records that can be stored in the array of structures is 10\n");
printf("Cannot accommodate more records....\n");
fclose(file_pointer);
return;
}
rewind(file_pointer);
fread(&emp, sizeof(struct employee), no_of_records, file_pointer);
printf("The following information is present in the employee file:\n");
for (i = 0; i < no_of_records; i++) {
printf("Employee code: %d\n", emp[i].employee_code);
printf("Employee name: %s\n", emp[i].employee_name);
printf("Designation: %s\n", emp[i].designation);
printf("Salary: %f\n", emp[i].salary);
}
fclose(file_pointer);
}
This program reads employee records from a file named employee.dat and stores them in an array of structures. The program defines an employee structure with fields for employee code, name, designation, and salary. The function disprecords() is responsible for opening the file, counting the number of records, and ensuring that no more than 10 records are stored in the array. It uses fread() to read the file contents into an array of employee structures. If the file contains more than 10 records, it prints a warning and stops. Finally, it displays the employee details (code, name, designation, and salary) for each record and closes the file after reading.
Summary
In C programming, functions can be implemented to add and display records efficiently using structures to store user details. A typical approach involves creating a structure to represent a user, with fields such as name, age, ID, or any relevant details. To add records, a function reads user input and stores the data in an array of structures or directly into a file. A separate display function can then retrieve these records, either from memory or a file, and print them to the console. This modular approach improves code organization, allowing seamless addition and retrieval of multiple records while managing data efficiently.