-1

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
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Where's the C#? Do you mean C++? – David L Dec 14 '14 at 02:34
  • my bad i just ment regular C. im using xcode – Jonathan Landry Dec 14 '14 at 02:39
  • 1
    See [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) for why you should not write a loop like that. In your defence, you are testing the `fgets()` calls too, which is commonly overlooked, so your code is not suffering from the standard problem. – Jonathan Leffler Dec 14 '14 at 03:15
  • 1
    You can't produce the report shown from the data shown because the name `Big Jim's Pizza Haus` doesn't appear in the data. OK; more precisely, you can only produce the report as shown by hard-coding the employee report for that one place. The `FILE:` line and the initial `REPORT` line in the data file are puzzling. Note that each call to `fgets()` will read an entire line (you've carefully made sure that your buffer size is much bigger than any plausible line of data; that's good). – Jonathan Leffler Dec 14 '14 at 03:25
  • 1
    If you're supposed to read a list of INSERT lines and store the values before producing the start of a report, you have some work to do. Note that one of the most basic techniques for working out what is going on in a program is to print what the program just read, so you know what the program saw (and you can compare that with what you expected the program to see). This can often set you on the right path. – Jonathan Leffler Dec 14 '14 at 03:26

1 Answers1

0

fgets() reads as many characters as you ask it to (up to the end of the line); you are treating it as if it reads only one item with each call. You get 0 for every ID b/c you try to parse the whole line as a number, which always starts with a word; you then read then next line, and set name to that; then read the next 2 lines, trying to parse each as a number (see above). So you read 3 extra lines for each Employee, which is why you skip 3 each time. (And why do you malloc Employee, only to free it at the end of each iteration?)

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • Is there something that does treat it like that thought. cause my end result im trying to do for my assignment is suppose to look like this. (il put it at the bottom of code) – Jonathan Landry Dec 14 '14 at 02:56