Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

AIM: . an array of record contains information of managers and workers .

print all data of


managers and workers in seprete file. (1) structure

(2) using union

Software used: VS Code


Algorithm:
1. Include Necessary Header:
 The code includes the necessary header file <stdio.h> for input/output
operations.
2. Define Structures:
 Defines two structures struct Manager and struct Worker to represent the
data for managers and workers, respectively.
 Each structure contains fields such as name, ID, and salary/hourly rate.
3. Define Union:
 Defines a union union Record that can hold either a struct Manager or a
struct Worker.
 This union allows flexibility in storing different types of employee data
in the same array.
4. Main Function:
 The main function initializes an array employees of type union Record to
store both manager and worker data.
5. Initialize Manager Data:
 Uses a loop to initialize data for two managers in the employees array.
 Asks the user to input details such as name, manager ID, and salary for
each manager.
6. Initialize Worker Data:
 Uses another loop to initialize data for three workers in the employees
array.
 Asks the user to input details such as name, worker ID, and hourly rate
for each worker.
7. Print Manager Data to File:
 Opens a file named "managers.txt" for writing.
 Prints the manager data from the employees array to the file, including
name, manager ID, and salary.
8. Print Worker Data to File:
 Opens a file named "workers.txt" for writing.
 Prints the worker data from the employees array to the file, including
name, worker ID, and hourly rate.
9. Error Handling for File Operations:
 Provides error handling for file opening operations and returns
appropriate error codes if there are issues opening the files.
10. Print Success Message:
 If the program reaches this point, it prints a success message indicating
that the data has been written to files.
11. Return Codes:
 Returns 1 or 2 in case of errors during file opening operations.
 Returns 0 in case of successful execution.

Flowchart :
SOURCE CODE :
#include <stdio.h>

// Define structures for manager and worker


struct Manager {
char name[50];
int managerID;
float salary;
};

struct Worker {
char name[50];
int workerID;
float hourlyRate;
};

// Define a union for either a manager or a worker


union Record {
struct Manager managerData;
struct Worker workerData;
};

int main() {
// Create an array of records that can store both managers and workers
union Record employees[5]; // Assuming there are 5 records for
demonstration

// Initialize manager data


for (int i = 0; i < 2; ++i) {
printf("Enter Manager #%d details:\n", i + 1);
printf("Name: ");
scanf("%s", employees[i].managerData.name);
printf("Manager ID: ");
scanf("%d", &employees[i].managerData.managerID);
printf("Salary: ");
scanf("%f", &employees[i].managerData.salary);
}

// Initialize worker data


for (int i = 2; i < 5; ++i) {
printf("Enter Worker #%d details:\n", i - 1);
printf("Name: ");
scanf("%s", employees[i].workerData.name);
printf("Worker ID: ");
scanf("%d", &employees[i].workerData.workerID);
printf("Hourly Rate: ");
scanf("%f", &employees[i].workerData.hourlyRate);
}

// Print manager data to a file


FILE *managerFile = fopen("managers.txt", "w");
if (managerFile == NULL) {
perror("Error opening managers.txt");
return 1;
}

fprintf(managerFile, "Manager Data:\n");


for (int i = 0; i < 2; ++i) {
fprintf(managerFile, "Name: %s\n", employees[i].managerData.name);
fprintf(managerFile, "Manager ID: %d\n",
employees[i].managerData.managerID);
fprintf(managerFile, "Salary: %.2f\n",
employees[i].managerData.salary);
fprintf(managerFile, "\n");
}

fclose(managerFile);

// Print worker data to a file


FILE *workerFile = fopen("workers.txt", "w");
if (workerFile == NULL) {
perror("Error opening workers.txt");
return 2;
}

fprintf(workerFile, "Worker Data:\n");


for (int i = 2; i < 5; ++i) {
fprintf(workerFile, "Name: %s\n", employees[i].workerData.name);
fprintf(workerFile, "Worker ID: %d\n",
employees[i].workerData.workerID);
fprintf(workerFile, "Hourly Rate: %.2f\n",
employees[i].workerData.hourlyRate);
fprintf(workerFile, "\n");
}

fclose(workerFile);

printf("Data written to files: managers.txt and workers.txt\n");

return 0;
}

OUTPUT :

You might also like