In my C code, employee.h
looks like this:
typedef struct Employee {
int Id;
char Name[100];
int Age;
long Salary;
}Employee;
What I'm trying to do is create a report that will show the id, employee, age, and salary. But I can't seem to figure out how to separate each of those things from the file. Also my output keeps skipping lines in the file which I'm sure will be fixed if I can just separate them like I plan to. File and output will be at the bottom to show how it looks. This is only apart of my assignment but figuring this out will help me be able to finish the rest. Hopefully someone from here can help me out.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"employee.h"
void printemployeeByPointer(Employee * e) {
printf("Id:%d Name:%s Age:%d Salary:%ld\n",e->Id,e->Name,e->Age,e->Salary);
}
void stripNewLine(char * s) {
int len = strlen(s);
if(len < 1)
return;
if(*(s + len - 1) == '\n')
*(s + len - 1) = '\0';
}
int main(){
int i;
int counter=0;
char buffer[2000];
Employee * Emp;
//open .txt
FILE * fp = fopen("file.txt", "r");
if(fp == NULL) {
printf("cannot open file!\n");
return EXIT_FAILURE;
}
//read in (2 lines in our file per fruit)
while(!feof(fp)) {
//make a heap
Emp = (Employee *) malloc(sizeof(Employee));
//check null
if(Emp == NULL) {
//skip this iteration
}
//read in Id
if(fgets(buffer, 2000, fp) == NULL) {
//if fgets returns NULL then we have a problem with file
//so skip this iteration
}
Emp->Id = atoi(buffer);
//read in name
if(fgets(buffer, 2000, fp) == NULL) {
//if fgets returns NULL then we have a problem with file
//so skip this iteration
}
stripNewLine(buffer);
strcpy(Emp->Name, buffer);
//read in Age
if(fgets(buffer, 2000, fp) == NULL) {
//if fgets returns NULL then we have a problem with file
//so skip this iteration
}
Emp->Age = atoi(buffer);
//read in Salary
if(fgets(buffer, 2000, fp) == NULL) {
//if fgets returns NULL then we have a problem with file
//so skip this iteration
}
Emp->Salary = atoi(buffer);
printemployeeByPointer(Emp);
free(Emp);
counter++;
}
//close file
fclose(fp);
return EXIT_SUCCESS;
}
Example input:
FILE:
REPORT
INSERT 1|Bob Roberts|42|35040
INSERT 2|Maxwell Smart|37|21000
REPORT
INSERT 3|Sue Jones|40|27000
INSERT 4|Ralph Thomson|21|25000
INSERT 5|Betty Zanzibar|28|28500
REPORT
INSERT 6|Ragnar Smith|62|26000
REPORT
INSERT 20|Lou Wilson|40|17000
INSERT 21|Wilma Thomas|35|21000
INSERT 22|Robert Roberts|37|20500
INSERT 23|Louise Cranberry|23|18600
REPORT
INSERT 30|Richard Hamburger|28|29000
INSERT 31|Raoul Gorgonzola|31|24200
REPORT
INSERT 41|Joan Johnson|32|15000
INSERT 42|Simon Smith|27|17000
REPORT
INSERT 43|Tammy Patterson|30|27000
INSERT 44|Phillip Morris|21|15500
INSERT 45|Morris Phillips|25|20500
REPORT
INSERT 46|Rhonda Rhodes|22|26000
REPORT
INSERT 50|Winona Wombat|30|23000
INSERT 51|Gary Busey|70|350000
INSERT 52|Charles the Zealot|34|11200
INSERT 53|Horace Horatio|41|18600
REPORT
INSERT 60|Carrot Top|49|150
INSERT 61|Andy Adams|31|20200
REPORT
MYOUTPUT:
Id:0 Name:INSERT 1|Bob Roberts|42|35040 Age:0 Salary:0
Id:0 Name:INSERT 4|Ralph Thomson|21|25000 Age:0 Salary:0
Id:0 Name:REPORT Age:0 Salary:0
Id:0 Name:INSERT 23|Louise Cranberry|23|18600 Age:0 Salary:0
Id:0 Name:REPORT Age:0 Salary:0
Id:0 Name:INSERT 43|Tammy Patterson|30|27000 Age:0 Salary:0
Id:0 Name:INSERT 46|Rhonda Rhodes|22|26000 Age:0 Salary:0
Id:0 Name:INSERT 52|Charles the Zealot|34|11200 Age:0 Salary:0
Id:0 Name:INSERT 61|Andy Adams|31|20200 Age:0 Salary:0
Finished result I'm working my way towards:
Employee Report for Big Jim's Pizza Haus
Id Name Age Salary
------ --------------------------- ----- --------------
1 Bob Roberts 42 35040.00
2 Maxwell Smart 37 21000.00
Total Employees: 2
Total salary: 56040.00
Average salary: 28020.00