如何将文件中的单独内容放入不同的数组中,以便打印出我需要它的方式?

时间:2014-12-14 02:33:28

标签: c arrays file inventory

在我的C代码中,employee.h看起来像这样:

typedef struct Employee {
    int Id;
    char Name[100];
    int Age;
    long Salary;
}Employee;

我要做的是创建一个显示身份证,员工,年龄和薪水的报告。但我似乎无法弄清楚如何将每个东西与文件分开。此外,我的输出一直在跳过文件中的行,如果我可以像我计划的那样将它们分开,我肯定会修复它。文件和输出将位于底部以显示其外观。这只是我的任务的一部分,但搞清楚这将有助于我完成剩下的工作。希望有来自这里的人可以帮助我。

#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;
}

示例输入:

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

完成的结果我正朝着:

努力
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

1 个答案:

答案 0 :(得分:0)

fgets()读取你要求的字符数(直到行尾);你正在对待它,好像它每次调用只读取一个项目。你尝试将整行解析为一个数字,每个ID b / c得到0,这个数字总是以一个单词开头;然后你读下一行,然后设置名称;然后阅读接下来的两行,尝试将每个行解析为一个数字(见上文)。所以你为每个员工读了3行,这就是你每次跳过3行的原因。 (为什么你要malloc员工,只在每次迭代结束时释放它?)