Untitled

 avatar
unknown
plain_text
5 months ago
10 kB
2
Indexable
#include <stdio.h>
#include <stdlib.h>
#include "employee.h"

// Define the employeeCount variable here
int employeeCount = 0;

int main() {
    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
                break;
            case 2:
                {
                    int searchId;
                    printf("Enter Employee ID to search: ");
                    scanf("%d", &searchId);
                    searchEmployee(searchId);
                }
                break;
            case 3:
                {
                    int modifyId;
                    printf("Enter Employee ID to modify: ");
                    scanf("%d", &modifyId);
                    modifyEmployee(modifyId);
                }
                break;
            case 4:
                {
                    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;
}





#include <stdio.h>
#include <stdlib.h>
#include "employee.h"

// Function to modify employee information by ID
void modifyEmployee(int empid) {
    FILE *file = fopen("employees.txt", "r+b"); // Open file in read/update mode
    if (file == NULL) {
        printf("File does not exist.\n");
        return; // Exit if the file doesn't exist
    }

    struct Employee emp;
    int recordFound = 0;

    // Check if the file has records
    while (fread(&emp, sizeof(struct Employee), 1, file) == 1) {
        if (emp.empid == empid) {
            recordFound = 1;
            printf("Employee found. Enter new details.\n");

            // Collect new employee details (reusing collectEmployeeInfo function)
            collectEmployeeInfo(&emp);

            // Move file pointer to the beginning of the record and update it
            fseek(file, -sizeof(struct Employee), SEEK_CUR);
            fwrite(&emp, sizeof(struct Employee), 1, file);
            printf("Employee details updated successfully.\n");
            break;
        }
    }

    if (!recordFound) {
        printf("Employee with ID %d not found.\n", empid);
    }

    fclose(file); // Close the file
}


#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\n", emp->empid, emp->empname, emp->esalary, emp->designation);
        fclose(file);
    } else {
        printf("Error opening file for writing.\n");
    }
}


#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <stdio.h>
#include <stdlib.h>

struct Employee {
    int empid;
    char empname[100];      // Using fixed size for simplicity
    int esalary;
    char designation[100];  // Using fixed size for simplicity
};

// 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


#include <stdio.h>
#include "employee.h"

// Function to display all employees
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;
    int recordFound = 0;

    // Read employee records until EOF
    while (fscanf(file, "%d,%99[^,],%d,%99[^\n]\n", &emp.empid, emp.empname, &emp.esalary, emp.designation) == 4) {
        recordFound = 1; // Set flag if any record exists
        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("\n");
    }

    if (!recordFound) {
        printf("No records found in the file.\n");
    }

    fclose(file); // Close the file
}


#include <stdio.h>
#include <stdlib.h>
#include "employee.h"

// Function to delete an employee by ID
void deleteEmployee(int empid) {
    FILE *file = fopen("employees.txt", "rb"); // Open file in binary read mode
    if (file == NULL) {
        printf("File does not exist.\n");
        return; // Exit if the file doesn't exist
    }

    FILE *tempFile = fopen("temp.txt", "wb"); // Temporary file for storing remaining records
    struct Employee emp;
    int recordFound = 0;

    // Read employee records until EOF using binary format
    while (fread(&emp, sizeof(struct Employee), 1, file) == 1) {
        if (emp.empid != empid) {
            // Write to temp file if not deleting
            fwrite(&emp, sizeof(struct Employee), 1, tempFile);
        } else {
            recordFound = 1; // Set flag if record is found and marked for deletion
        }
    }

    fclose(file); // Close the original file
    fclose(tempFile); // Close the temporary file

    // Replace the original file with the temporary file
    if (recordFound) {
        remove("employees.txt"); // Remove the old file
        rename("temp.txt", "employees.txt"); // Rename temp file to original
        printf("Employee with ID %d deleted successfully.\n", empid);
        employeeCount--; // Decrement employee count
    } else {
        printf("Employee with ID %d not found.\n", empid);
        remove("temp.txt"); // Remove temp file if no deletion occurred
    }
}

#include <stdio.h>
#include <stdlib.h>
#include "employee.h"

// Function to search for an employee by ID
void searchEmployee(int empid) {
    FILE *file = fopen("employees.txt", "r"); // Open file in binary read mode
    if (file == NULL) {
        printf("File does not exist.\n");
        return; // Exit the function if the file doesn't exist
    }

    struct Employee emp;
    int found = 0;

    // Read employee records until EOF
    while (fread(&emp, sizeof(struct Employee), 1, file) == 1) {
        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);
            found = 1; // Set found flag
            break; // Exit loop if employee is found
        }
    }

    if (!found) {
        printf("Employee with ID %d not found.\n", empid);
    }

    fclose(file); // Close the file
}

#include "employee.h"
#include <string.h>
#include <ctype.h>

// Global variable to keep track of the number of employees
// int employeeCount = 0;

// 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) {
    fgets(input, sizeof(input), 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 check if Employee ID is a duplicate
int isDuplicateID(int empid) {
    FILE *file = fopen("employees.txt", "rb"); // Open file in binary read mode
    if (file == NULL) {
        return 0; // File does not exist, no duplicates
    }

    struct Employee emp;
    while (fread(&emp, sizeof(struct Employee), 1, file) == 1) {
        if (emp.empid == empid) {
            fclose(file);
            return 1; // Duplicate ID found
        }
    }

    fclose(file); // Close the file
    return 0; // No duplicates
}

void collectEmployeeInfo(struct Employee *emp) {
    // Validate and collect Employee ID
    printf("Enter Employee ID (integer): ");
    while (1) {
        emp->empid = validateIntegerInput();
        if (emp->empid == -1) {
            continue; // Invalid input, loop again
        }
        if (isDuplicateID(emp->empid)) {
            printf("ID already exists. Try again.\n");
        } else {
            break; // Valid and unique ID found
        }
    }

    // Validate and collect Employee Name
    printf("Enter Employee Name (alphabetic only): ");
    while (!validateStringInput(emp->empname));  // Loop until valid input is provided

    // Validate and collect Employee Salary
    printf("Enter Employee Salary (integer): ");
    while ((emp->esalary = validateIntegerInput()) == -1);  // Loop until valid input is provided

    // Validate and collect Designation
    printf("Enter Designation (alphabetic only): ");
    while (!validateStringInput(emp->designation));  // Loop until valid input is provided

    employeeCount++; // Increment the employee count when a new record is collected
}

Editor is loading...
Leave a Comment