1 year ago

#371259

test-img

johnny734

C file processing assignment question, why is this error coming up?

I am currently trying to finish an assignment and cannot seem to get my code running completely. Not entirely sure what is going on with it as our teacher is not much help in class. Any help or suggestions would be greatly appreciated.

#include <stdio.h>
struct EmployeeData {
    unsigned int ID;
    char lastName[15];
    char firstName[15];
    char department[15];
    char position[15];
    char address[15];
    char email[20];
    double salary;
};
unsigned int Menu();
void newRecord(FILE* fPtr);
int main(void) {
    FILE* epsPtr;
    unsigned int menu;

    epsPtr = fopen_s(&epsPtr, "employee.dat", "wb+");
    printf("*** Welcome to the Employee Portal ***\n");

    if (epsPtr != 0) {
        puts("File could not be opened");
    }
    else {
        while ((menu = Menu()) != 8) {
            switch (menu) {
            case 1:
                newRecord(epsPtr);
                break;
            }
        }
    }
}
unsigned int Menu() {
    printf("Enter your choice:\n1 - Insert an employee record\n2 - Update existing info of an employee record\n");
    printf("3 - Remove an employee record\n4 - Process the binary file for printing\n5 - Show all employee records\n");
    printf("6 - Statistics of employees\n7 - Exit\n? ");
    unsigned int menuChoice;
    scanf_s("%u", &menuChoice);
    return menuChoice;
}
void newRecord(FILE* fPtr) {
    struct EmployeeData employee = { 0, "", "", "", "", "", "", 0.0 };
    unsigned int employeeID;
    printf("Enter employee ID: ");
    scanf_s("%d", &employeeID);

    fseek(fPtr, (employeeID - 1) * sizeof(struct EmployeeData), SEEK_SET);
    fread(&employee, sizeof(struct EmployeeData), 1, fPtr);

    if (employee.ID != 0) {
        printf("Record #%d already exists.\n", employee.ID);
    }
    else {
        printf("Enter last name, first name, department, position, address, email and salary of the employee");
        scanf_s("%14c%14c%14c%14c%14c%19c%lf", &employee.lastName, &employee.firstName, &employee.department, &employee.position, &employee.address, &employee.email, &employee.salary);
        employee.ID = employeeID;

        fseek(fPtr, (employee.ID - 1) * sizeof(struct EmployeeData), SEEK_SET);
        fwrite(&employee, sizeof(struct EmployeeData), 1, fPtr);
    }
}

Error I get when trying to run through code

c

windows

file-processing

0 Answers

Your Answer

Accepted video resources