Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I keep getting this message when I run my program File payfile.txt could not be opened for input Here Is my code- this is for

I keep getting this message when I run my program "File payfile.txt could not be opened for input"

Here Is my code- this is for C programming using visual studio 2019

#include #include #include

#define MAX 100

FILE* fpIn; FILE* fpOut;

struct Employee { char first[8]; char initial[2]; char last[10]; char street[17]; char city[12]; char state[3]; char zip[6]; int age; // integer char sex[2]; // 1 character max (m/f) int tenure; // integer representing years of emaployment float salary; // double representing weekly salary }Employee;

void strsub(char buf[], char sub[], int start, int end); int inputData(FILE* fpIn, struct Employee workers[MAX]); void data(struct Employee workers[MAX], int count); void menEmployees(struct Employee workers[MAX], int count); void highestPaidWoman(struct Employee workers[MAX], int count); void lowestPaidMan(struct Employee workers[MAX], int count); void averageSalary(struct Employee workers[MAX], int count, float average); void belowAverageWomen(struct Employee workers[MAX], int count, float average); void menSalaryRatio(struct Employee workers[MAX], int count, float average); void employeeSalaryYearAge(struct Employee workers[MAX], int count); void sort(struct Employee workers[MAX], int count); void employee10PercentRaise(struct Employee workers[MAX], int count); void employeeBySortedZipCode(struct Employee workers[MAX], int count);

int main() { struct Employee workers[MAX]; int count = 0; float average = 0;

count = inputData(fpIn, workers); data(workers, count); menEmployees(workers, count); highestPaidWoman(workers, count); lowestPaidMan(workers, count); averageSalary(workers, count, average); belowAverageWomen(workers, count, average); menSalaryRatio(workers, count, average); employeeSalaryYearAge(workers, count); employee10PercentRaise(workers, count); employeeBySortedZipCode(workers, count); }

void strsub(char buf[], char sub[], int start, int end) { int i, j;

for (j = 0, i = start; i <= end; i++, j++) sub[j] = buf[i];

sub[j] = '\0'; } int inputData(FILE* fpIn,struct Employee workers[MAX]) { int i = 0; char buf[MAX]; char temp[MAX];

fpOut = fopen_s(&fpOut, "csis.txt", "w"); fpIn = fopen_s(&fpIn, "payfile.txt", "r"); if (fpIn == NULL) { puts("File payfile.txt could not be opened for input."); exit(1); } while (!feof(fpIn)) { fgets(buf, MAX, fpIn); strsub(buf, workers[i].first, 0, 6); strsub(buf, workers[i].initial, 8, 8); strsub(buf, workers[i].last, 10, 18); strsub(buf, workers[i].street, 20, 35); strsub(buf, workers[i].city, 37, 47); strsub(buf, workers[i].state, 49, 50); strsub(buf, workers[i].zip, 52, 56); strsub(buf, temp, 58, 59); workers[i].age = atoi(temp); strsub(buf, workers[i].sex, 61, 61); strsub(buf, temp, 63, 63); workers[i].tenure = atoi(temp); strsub(buf, temp, 65, 70); workers[i].salary = atof(temp); ++i; } fclose(fpOut); fclose(fpIn);

return i; return 0; }

void data(struct Employee workers[MAX], int count)

{ printf("Details of all the Employees: "); fprintf(fpIn,"Details of all the Employees: "); for (int i = 0; i < count; i++) { printf("%-7s %-s %-9s %-16s %-11s %-2s %-5s %d %s %d %.2f ", workers[i].first, workers[i].initial, workers[i].last, workers[i].street, workers[i].city, workers[i].state, workers[i].zip, workers[i].age, workers[i].sex, workers[i].tenure, workers[i].salary);

fprintf(fpIn, "%-7s %-s %-9s %-16s %-11s %-2s %-5s %d %s %d %.2f ", workers[i].first, workers[i].initial, workers[i].last, workers[i].street, workers[i].city, workers[i].state, workers[i].zip, workers[i].age, workers[i].sex, workers[i].tenure, workers[i].salary); } } void menEmployees(struct Employee workers[MAX], int count) { printf(" First and Last Names of all the Men Employees: "); fprintf(fpIn, " First and Last Names of all the Men Employees: "); for (int i = 0; i < count; i++) { if (strcmp(workers[i].sex, "M") == 0) { printf("%-7s %-9s ", workers[i].first, workers[i].last); fprintf(fpIn, "%-7s %-9s ", workers[i].first, workers[i].last); } } } void highestPaidWoman(struct Employee workers[MAX], int count) { float highestPaidWoman = -1; for (int i = 0; i < count; i++) { if (strcmp(workers[i].sex, "F") == 0) { if (highestPaidWoman < 0) { highestPaidWoman = workers[i].salary; } else if (workers[i].salary > highestPaidWoman) { highestPaidWoman = workers[i].salary; } } } printf(" First and Last Names of the Highest Paid Woman: "); fprintf(fpIn, " First and Last Names of the Highest Paid Woman: "); for (int i = 0; i < count; i++) { if (strcmp(workers[i].sex, "F") == 0 && workers[i].salary == highestPaidWoman) { printf("%-7s %-9s ", workers[i].first, workers[i].last); fprintf(fpIn, "%-7s %-9s ", workers[i].first, workers[i].last); } } } void lowestPaidMan(struct Employee workers[MAX], int count) { float lowestPaidMan = -1; for (int i = 0; i < count; i++) { if (strcmp(workers[i].sex, "M") == 0) { if (lowestPaidMan < 0) { lowestPaidMan = workers[i].salary; } else if (workers[i].salary < lowestPaidMan) { lowestPaidMan = workers[i].salary; } } } printf(" First and Last Names of the Lowest Paid Man: "); fprintf(fpIn, " First and Last Names of the Lowest Paid Man: "); for (int i = 0; i < count; i++) { if (strcmp(workers[i].sex, "M") == 0 && workers[i].salary == lowestPaidMan) { printf("%-7s %-9s ", workers[i].first, workers[i].last); fprintf(fpIn, "%-7s %-9s ", workers[i].first, workers[i].last); } } } void averageSalary(struct Employee workers[MAX], int count, float average) { float total = 0; for (int i = 0; i < count; i++) { total = total + workers[i].salary; } average = total / count; printf(" Average Salary of all the Employees: %.2f ", average); fprintf(fpIn," Average Salary of all the Employees: %.2f ", average); } void belowAverageWomen(struct Employee workers[MAX], int count, float average) { printf(" First and Last Names of Women Earning Below the Average Salary: "); fprintf(fpIn," First and Last Names of Women Earning Below the Average Salary: "); for (int i = 0; i < count; i++) { if (strcmp(workers[i].sex, "F") == 0 && workers[i].salary < average) { printf("%-7s %-9s ", workers[i].first, workers[i].last); fprintf(fpIn, "%-7s %-9s ", workers[i].first, workers[i].last); } } } void menSalaryRatio(struct Employee workers[MAX], int count, float average) { int above = 0; int below = 0; int gcd = 1; for (int i = 0; i < count; i++) { if (strcmp(workers[i].sex, "M") == 0 && workers[i].salary > average) { above++; } if (strcmp(workers[i].sex, "M") == 0 && workers[i].salary < average) { below++; } } int val1 = above; int val2 = below; while (val1 != val2) { if (val1 > val2) val1 -= val2; else val2 -= val1; } gcd = val1; printf(" First and Last Names of Women Earning Below the Average Salary: "); fprintf(fpIn, " First and Last Names of Women Earning Below the Average Salary: "); } void employeeSalaryYearAge(struct Employee workers[MAX], int count) { printf(" First and Last Names of Employees Who Make More Than $35,000 Per Year, Have Been with the Company for at Least 5 Years, and Who Wre Over 30 Years Old: "); fprintf(fpIn, " First and Last Names of Employees Who Make More Than $35,000 Per Year, Have Been with the Company for at Least 5 Years, and Who Wre Over 30 Years Old: "); for (int i = 0; i < count; i++) { if (workers[i].salary * 52 > 35000 && workers[i].tenure >= 5 && workers[i].age > 30) { printf("%-7s %-9s ", workers[i].first, workers[i].last); fprintf(fpIn, "%-7s %-9s ", workers[i].first, workers[i].last); } } } void employee10PercentRaise(struct Employee workers[MAX], int count) { printf(" First and Last Names and New Salary of the Employees with Raise: "); fprintf(fpIn, " First and Last Names and New Salary of the Employees with Raise: "); for (int i = 0; i < count; i++) { if (workers[i].salary < 350) { workers[i].salary += (workers[i].salary * 0.10); printf("%-7s %-9s %.2f ", workers[i].first, workers[i].last, workers[i].salary); fprintf(fpIn, "%-7s %-9s %.2f ", workers[i].first, workers[i].last, workers[i].salary); } } } void sort(struct Employee workers[MAX], int count) { int i, j; struct Employee temp;

for (i = 1; i < count; i++) { temp = workers[i]; j = i - 1;

while (j >= 0 && strcmp(temp.zip, workers[j].zip) < 0) { workers[j + 1] = workers[j]; j = j - 1; }

workers[j + 1] = temp; } } void employeeBySortedZipCode(struct Employee workers[MAX], int count) { sort(workers, count); printf(" First and Last Names and Zip Codes of Employees After Sorting By Zip Code: "); fprintf(fpIn, " First and Last Names and Zip Codes of Employees After Sorting By Zip Code: "); for (int i = 0; i < count; i++) { printf("%-7s %-9s %-5s ", workers[i].first, workers[i].last, workers[i].zip); fprintf(fpIn, "%-7s %-9s %-5s ", workers[i].first, workers[i].last, workers[i].zip); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions