Untitled
unknown
plain_text
5 months ago
8.3 kB
4
Indexable
Here are the modifications needed to add the `SEARCHBYCITY` functionality and rename the existing search function to `searchByID`. I'll create a new file for the `searchByCity` function and make necessary changes in other files as well. ### 1. Update the `Employee` Structure in `employee.h` First, we'll add the `Address` structure inside the `Employee` structure to hold address details. ```c //employee.h #ifndef EMPLOYEE_H #define EMPLOYEE_H #include <stdio.h> #include <stdlib.h> struct Address { char houseno[100]; char street[100]; char city[100]; int pincode; }; struct Employee { int empid; char *empname; // Pointer for employee name int esalary; char *designation; // Pointer for designation struct Address address; // Address structure }; // Declare the employeeCount variable as extern extern int employeeCount; void collectEmployeeInfo(struct Employee *emp); void searchByID(int empid); void searchByCity(const char *city); void modifyEmployee(int empid); void deleteEmployee(int empid); void displayAllEmployees(); void saveToFile(struct Employee *emp); #endif ``` ### 2. Modify `collect.c` to Collect Address Information Update `collectEmployeeInfo` to include input for the address fields. ```c //collect.c #include "employee.h" #include <string.h> #include <ctype.h> // Existing validation functions remain unchanged... void collectEmployeeInfo(struct Employee *emp) { emp->empname = malloc(100 * sizeof(char)); emp->designation = malloc(100 * sizeof(char)); if (emp->empname == NULL || emp->designation == NULL) { printf("Memory allocation failed.\n"); exit(1); } // Collect Employee ID, Name, Salary, and Designation... // Collect Address Information printf("Enter House Number: "); fgets(emp->address.houseno, sizeof(emp->address.houseno), stdin); emp->address.houseno[strcspn(emp->address.houseno, "\n")] = '\0'; // Remove newline printf("Enter Street: "); fgets(emp->address.street, sizeof(emp->address.street), stdin); emp->address.street[strcspn(emp->address.street, "\n")] = '\0'; // Remove newline printf("Enter City: "); fgets(emp->address.city, sizeof(emp->address.city), stdin); emp->address.city[strcspn(emp->address.city, "\n")] = '\0'; // Remove newline printf("Enter Pincode: "); emp->address.pincode = validateIntegerInput(); // Validate and collect Pincode employeeCount++; // Increment the employee count when a new record is collected } ``` ### 3. Rename `searchEmployee` to `searchByID` and Create `searchByCity` Function Create a new file `searchByCity.c` and modify the existing search function in `search.c`. ```c //search.c #include <stdio.h> #include <stdlib.h> #include "employee.h" // Function to search for an employee by ID void searchByID(int empid) { FILE *file = fopen("employees.txt", "r"); if (file == NULL) { printf("File does not exist.\n"); return; } fseek(file, 0, SEEK_END); if (ftell(file) == 0) { printf("No records found.\n"); fclose(file); return; } fseek(file, 0, SEEK_SET); struct Employee emp; emp.empname = malloc(100 * sizeof(char)); emp.designation = malloc(100 * sizeof(char)); if (emp.empname == NULL || emp.designation == NULL) { printf("Memory allocation failed.\n"); fclose(file); return; } int found = 0; while (fscanf(file, "%d,%99[^,],%d,%99[^,],%99[^,],%99[^,],%d\n", &emp.empid, emp.empname, &emp.esalary, emp.designation, emp.address.houseno, emp.address.street, &emp.address.pincode) == 7) { 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: %s, %s, %s, %d\n", emp.address.houseno, emp.address.street, emp.address.city, emp.address.pincode); found = 1; // Set found flag break; // Exit loop if employee is found } } if (!found) { printf("Employee with ID %d not found.\n", empid); } free(emp.empname); free(emp.designation); fclose(file); } ``` ### 4. Implement `searchByCity` in `searchByCity.c` Create a new file `searchByCity.c` for searching employees by city. ```c //searchByCity.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include "employee.h" // Function to search for employees by city void searchByCity(const char *city) { FILE *file = fopen("employees.txt", "r"); if (file == NULL) { printf("File does not exist.\n"); return; } fseek(file, 0, SEEK_END); if (ftell(file) == 0) { printf("No records found.\n"); fclose(file); return; } fseek(file, 0, SEEK_SET); struct Employee emp; emp.empname = malloc(100 * sizeof(char)); emp.designation = malloc(100 * sizeof(char)); if (emp.empname == NULL || emp.designation == NULL) { printf("Memory allocation failed.\n"); fclose(file); return; } int found = 0; while (fscanf(file, "%d,%99[^,],%d,%99[^,],%99[^,],%99[^,],%d\n", &emp.empid, emp.empname, &emp.esalary, emp.designation, emp.address.houseno, emp.address.street, &emp.address.pincode) == 7) { if (strcmp(emp.address.city, city) == 0) { 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: %s, %s, %s, %d\n", emp.address.houseno, emp.address.street, emp.address.city, emp.address.pincode); found = 1; // Set found flag } } if (!found) { printf("No employees found in city: %s\n", city); } free(emp.empname); free(emp.designation); fclose(file); } ``` ### 5. Update the Main Menu in `main.c` Add an option to search by city in the main menu. ```c //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 by ID\n3. Search Employee by City\n4. Modify Employee\n5. Delete Employee\n6. Display All Employees\n7. 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 break; case 2: { int searchId; printf("Enter Employee ID to search: "); scanf("%d", &searchId); searchByID(searchId); break; } case 3: { char city[100]; printf("Enter city to search: "); fgets(city, sizeof(city), stdin); city[strcspn(city, "\n")] = '\0'; // Remove newline searchByCity(city); break; } case 4: // Modify Employee code remains unchanged... break; case 5: // Delete Employee code remains unchanged... break; case 6: displayAllEmployees(); break; case 7: exit(0); default: printf("Invalid choice. Please try again.\n"); } } return 0; } ``` ### Summary of Changes 1. Updated the `
Editor is loading...
Leave a Comment