Untitled
unknown
plain_text
a year ago
15 kB
5
Indexable
//collect.c
#include "employee.h"
#include <string.h>
#include <ctype.h>
// Function to validate integer input
int validateIntegerInput() {
int num;
char temp;
if (scanf("%d%c", &num, &temp) != 2 || temp != '\n') {
while ((temp = getchar()) != '\n' && temp != EOF); // Clear input buffer
printf("Invalid input. Please enter an integer.\n");
return -1; // Return error code for invalid input
}
return num; // Return valid integer
}
// Function to validate string input (alphabetic only)
int validateStringInput(char *input, size_t size) {
fgets(input, size, stdin);
input[strcspn(input, "\n")] = '\0'; // Remove newline
// Check if the string contains only alphabetic characters and spaces
for (int i = 0; i < strlen(input); i++) {
if (!isalpha(input[i]) && input[i] != ' ') {
printf("Invalid input. Please enter alphabetic characters only.\n");
return 0; // Return 0 for invalid input
}
}
return 1; // Return 1 for valid input
}
// Function to collect Address information
void collectAddress(struct Address *addr) {
printf("Enter House No: ");
addr->houseno = validateIntegerInput();
printf("Enter Street Name: ");
while (!validateStringInput(addr->street, 100));
printf("Enter City: ");
while (!validateStringInput(addr->city, 50));
printf("Enter Pincode: ");
addr->pincode = validateIntegerInput();
}
void collectEmployeeInfo(struct Employee *emp) {
emp->empname = malloc(100 * sizeof(char)); // Allocate memory for empname
emp->designation = malloc(100 * sizeof(char)); // Allocate memory for designation
if (emp->empname == NULL || emp->designation == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
// Validate and collect Employee ID
printf("Enter Employee ID (integer): ");
emp->empid = validateIntegerInput();
// Validate and collect Employee Name
printf("Enter Employee Name (alphabetic only): ");
while (!validateStringInput(emp->empname, 100));
// Validate and collect Employee Salary
printf("Enter Employee Salary (integer): ");
emp->esalary = validateIntegerInput();
// Validate and collect Designation
printf("Enter Designation (alphabetic only): ");
while (!validateStringInput(emp->designation, 100));
// Collect Address details
collectAddress(&emp->address);
employeeCount++; // Increment the employee count when a new record is collected
}
//delete.c
#include <stdio.h>
#include <stdlib.h>
#include "employee.h"
void deleteEmployee(int empid) {
FILE *file = fopen("employees.txt", "r"); // Open file in read mode
if (file == NULL) {
printf("File does not exist.\n");
return;
}
struct Employee emp;
emp.empname = malloc(100 * sizeof(char)); // Allocate memory for empname
emp.designation = malloc(100 * sizeof(char)); // Allocate memory for designation
if (emp.empname == NULL || emp.designation == NULL) {
printf("Memory allocation failed.\n");
fclose(file);
return;
}
FILE *tempFile = fopen("temp.txt", "w"); // Temporary file for modified data
if (tempFile == NULL) {
printf("Error opening temporary file.\n");
free(emp.empname);
free(emp.designation);
fclose(file);
return;
}
int found = 0;
// Read employee records and copy them to temp file, excluding the one to delete
while (fscanf(file, "%d,%99[^,],%d,%99[^,],%d,%99[^,],%99[^,],%d\n",
&emp.empid, emp.empname, &emp.esalary, emp.designation,
&emp.address.houseno, emp.address.street, emp.address.city, &emp.address.pincode) == 8) {
if (emp.empid != empid) {
fprintf(tempFile, "%d,%s,%d,%s,%d,%s,%s,%d\n",
emp.empid, emp.empname, emp.esalary, emp.designation,
emp.address.houseno, emp.address.street, emp.address.city, emp.address.pincode);
} else {
found = 1; // Set found flag
}
}
fclose(file);
fclose(tempFile);
free(emp.empname);
free(emp.designation);
if (found) {
remove("employees.txt");
rename("temp.txt", "employees.txt");
employeeCount--; // Decrement employee count on successful deletion
printf("Employee deleted successfully.\n");
} else {
remove("temp.txt");
printf("Employee with ID %d not found.\n", empid);
}
}
//display.c
#include <stdio.h>
#include <stdlib.h>
#include "employee.h"
void displayAllEmployees() {
FILE *file = fopen("employees.txt", "r"); // Open file in read mode
if (file == NULL) {
printf("File does not exist.\n");
return; // Exit if the file doesn't exist
}
struct Employee emp;
emp.empname = malloc(100 * sizeof(char)); // Allocate memory for empname
emp.designation = malloc(100 * sizeof(char)); // Allocate memory for designation
if (emp.empname == NULL || emp.designation == NULL) {
printf("Memory allocation failed.\n");
fclose(file);
return;
}
int recordFound = 0;
// Print table header
printf("------------------------------------------------------------------------\n");
printf("| %-10s | %-20s | %-10s | %-20s | %-15s |\n", "Emp ID", "Name", "Salary", "Designation", "Address");
printf("------------------------------------------------------------------------\n");
// Read employee records until EOF
while (fscanf(file, "%d,%99[^,],%d,%99[^,],%d,%99[^,],%99[^,],%d\n",
&emp.empid, emp.empname, &emp.esalary, emp.designation,
&emp.address.houseno, emp.address.street, emp.address.city, &emp.address.pincode) == 8) {
recordFound = 1; // Set flag if any record exists
printf("| %-10d | %-20s | %-10d | %-20s | %d, %s, %s, %d |\n",
emp.empid, emp.empname, emp.esalary, emp.designation,
emp.address.houseno, emp.address.street, emp.address.city, emp.address.pincode);
}
if (!recordFound) {
printf("| %-10s | %-20s | %-10s | %-20s | %-15s |\n", "No records", "", "", "", "");
}
printf("------------------------------------------------------------------------\n");
free(emp.empname); // Free allocated memory
free(emp.designation); // Free allocated memory
fclose(file); // Close the file
}
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <stdio.h>
#include <stdlib.h>
struct Address {
int houseno;
char street[100];
char city[50];
int pincode;
};
struct Employee {
int empid;
char *empname; // Pointer for employee name
int esalary;
char *designation; // Pointer for designation
struct Address address; // Employee address
};
// Declare the employeeCount variable as extern
extern int employeeCount;
void collectEmployeeInfo(struct Employee *emp);
void searchEmployee(int empid);
void modifyEmployee(int empid);
void deleteEmployee(int empid);
void displayAllEmployees();
void saveToFile(struct Employee *emp);
#endif
//main.c
#include <stdio.h>
#include <stdlib.h>
#include "employee.h"
// Define the employeeCount variable here
int employeeCount = 0;
// Function to initialize employeeCount from the file
void initializeEmployeeCount() {
FILE *file = fopen("employees.txt", "rb");
if (file == NULL) {
employeeCount = 0;
return;
}
struct Employee emp;
while (fread(&emp, sizeof(struct Employee), 1, file) == 1) {
employeeCount++;
}
fclose(file);
}
int main() {
initializeEmployeeCount(); // Initialize employeeCount at the start
int choice;
struct Employee emp;
while (1) {
printf("\n1. Collect Employee Info\n2. Search Employee\n3. Modify Employee\n4. Delete Employee\n5. Display All Employees\n6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // Clear the newline from input buffer
switch (choice) {
case 1:
collectEmployeeInfo(&emp);
saveToFile(&emp); // Save employee data to the file
employeeCount++; // Increment employee count
break;
case 2:
if (employeeCount == 0) {
printf("No records found.\n");
break;
}
{
int searchId;
printf("Enter Employee ID to search: ");
scanf("%d", &searchId);
searchEmployee(searchId);
}
break;
case 3:
if (employeeCount == 0) {
printf("No records found.\n");
break;
}
{
int modifyId;
printf("Enter Employee ID to modify: ");
scanf("%d", &modifyId);
modifyEmployee(modifyId);
}
break;
case 4:
if (employeeCount == 0) {
printf("No records found.\n");
break;
}
{
int deleteId;
printf("Enter Employee ID to delete: ");
scanf("%d", &deleteId);
deleteEmployee(deleteId);
}
break;
case 5:
displayAllEmployees();
break;
case 6:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
//modify.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "employee.h"
int validateIntegerInput();
int validateStringInput(char *input, size_t size);
void collectAddress(struct Address *addr);
void modifyEmployee(int empid) {
FILE *file = fopen("employees.txt", "r+"); // Open file in read/update mode
if (file == NULL) {
printf("File does not exist.\n");
return;
}
struct Employee emp;
emp.empname = malloc(100 * sizeof(char)); // Allocate memory for empname
emp.designation = malloc(100 * sizeof(char)); // Allocate memory for designation
if (emp.empname == NULL || emp
.designation == NULL) {
printf("Memory allocation failed.\n");
fclose(file);
return;
}
FILE *tempFile = fopen("temp.txt", "w"); // Temporary file for modified data
if (tempFile == NULL) {
printf("Error opening temporary file.\n");
free(emp.empname);
free(emp.designation);
fclose(file);
return;
}
int found = 0;
// Read employee records
while (fscanf(file, "%d,%99[^,],%d,%99[^,],%d,%99[^,],%99[^,],%d\n",
&emp.empid, emp.empname, &emp.esalary, emp.designation,
&emp.address.houseno, emp.address.street, emp.address.city, &emp.address.pincode) == 8) {
if (emp.empid == empid) {
printf("Employee found. What do you want to modify?\n");
printf("1. Name\n2. Salary\n3. Designation\n4. Address\n");
int choice = validateIntegerInput();
switch (choice) {
case 1:
printf("Enter new name: ");
while (!validateStringInput(emp.empname, 100));
break;
case 2:
printf("Enter new salary: ");
emp.esalary = validateIntegerInput();
break;
case 3:
printf("Enter new designation: ");
while (!validateStringInput(emp.designation, 100));
break;
case 4:
printf("Enter new address details:\n");
collectAddress(&emp.address);
break;
default:
printf("Invalid choice. No changes made.\n");
}
found = 1;
}
fprintf(tempFile, "%d,%s,%d,%s,%d,%s,%s,%d\n",
emp.empid, emp.empname, emp.esalary, emp.designation,
emp.address.houseno, emp.address.street, emp.address.city, emp.address.pincode);
}
fclose(file);
fclose(tempFile);
free(emp.empname);
free(emp.designation);
if (found) {
remove("employees.txt");
rename("temp.txt", "employees.txt");
printf("Employee details updated successfully.\n");
} else {
remove("temp.txt");
printf("Employee with ID %d not found.\n", empid);
}
}
//save.c
#include <stdio.h>
#include "employee.h"
#define FILENAME "employees.txt"
void saveToFile(struct Employee *emp) {
FILE *file = fopen(FILENAME, "a"); // Open in append mode
if (file) {
fprintf(file, "%d,%s,%d,%s,%d,%s,%s,%d\n",
emp->empid, emp->empname, emp->esalary, emp->designation,
emp->address.houseno, emp->address.street, emp->address.city, emp->address.pincode);
fclose(file);
} else {
printf("Error opening file for writing.\n");
}
}
//search.c
#include <stdio.h>
#include <stdlib.h>
#include "employee.h"
void searchEmployee(int empid) {
FILE *file = fopen("employees.txt", "r"); // Open file in read mode
if (file == NULL) {
printf("File does not exist.\n");
return; // Exit if the file doesn't exist
}
struct Employee emp;
emp.empname = malloc(100 * sizeof(char)); // Allocate memory for empname
emp.designation = malloc(100 * sizeof(char)); // Allocate memory for designation
if (emp.empname == NULL || emp.designation == NULL) {
printf("Memory allocation failed.\n");
fclose(file);
return;
}
int found = 0;
// Read employee records until EOF
while (fscanf(file, "%d,%99[^,],%d,%99[^,],%d,%99[^,],%99[^,],%d\n",
&emp.empid, emp.empname, &emp.esalary, emp.designation,
&emp.address.houseno, emp.address.street, emp.address.city, &emp.address.pincode) == 8) {
if (emp.empid == empid) {
printf("Employee ID: %d\n", emp.empid);
printf("Employee Name: %s\n", emp.empname);
printf("Employee Salary: %d\n", emp.esalary);
printf("Designation: %s\n", emp.designation);
printf("Address: %d, %s, %s, %d\n",
emp.address.houseno, emp.address.street, emp.address.city, emp.address.pincode);
found = 1; // Set found flag
break;
}
}
if (!found) {
printf("Employee with ID %d not found.\n", empid);
}
free(emp.empname); // Free allocated memory
free(emp.designation); // Free allocated memory
fclose(file); // Close the file
}
Editor is loading...
Leave a Comment