C 如何将文件中的不同内容放入不同的数组中,使其按我需要的方式打印?
在我的C代码中,C 如何将文件中的不同内容放入不同的数组中,使其按我需要的方式打印?,c,arrays,file,inventory,C,Arrays,File,Inventory,在我的C代码中,employee.h如下所示: typedef struct Employee { int Id; char Name[100]; int Age; long Salary; }Employee; 我想做的是创建一个显示id、员工、年龄和工资的报告。但我似乎不知道如何将这些内容从文件中分离出来。此外,我的输出不断跳过文件中的行,如果我能像计划的那样将它们分开,我相信这些行将得到修复。文件和输出将位于底部,以显示其外观。这只是我任务的一部分,但弄清
employee.h
如下所示:
typedef struct Employee {
int Id;
char Name[100];
int Age;
long Salary;
}Employee;
我想做的是创建一个显示id、员工、年龄和工资的报告。但我似乎不知道如何将这些内容从文件中分离出来。此外,我的输出不断跳过文件中的行,如果我能像计划的那样将它们分开,我相信这些行将得到修复。文件和输出将位于底部,以显示其外观。这只是我任务的一部分,但弄清楚这一点将帮助我完成其余的任务。希望这里有人能帮我
#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;
}
我正在努力实现的最终结果:
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
fgets()读取您要求的字符数(直到行尾);您将其视为每次调用只读取一项。如果您尝试将整行解析为一个数字(总是以一个单词开头),则每个ID b/c都会得到0;然后阅读下一行,并将名称设置为该行;然后阅读接下来的两行,尝试将每一行解析为一个数字(见上文)。所以你为每个员工多读了3行,这就是为什么你每次跳过3行。(为什么你选择malloc员工,只是在每次迭代结束时释放它?C#在哪里?你是说C++吗?我的坏,我只是用C代码来写C。在您的辩护中,您也在测试
fgets()
调用,这通常被忽略,因此您的代码没有遇到标准问题。您无法根据显示的数据生成报告,因为数据中没有显示名称Big Jim's Pizza Haus
。好啊更准确地说,您只能通过硬编码该地点的员工报告来生成如图所示的报告。数据文件中的文件:
行和初始报告
行令人费解。请注意,每次调用fgets()
都将读取整行数据(您已仔细确保缓冲区大小比任何可能的数据行都大,这很好)。如果您要在生成报告开始之前读取插入行列表并存储值,则需要做一些工作。请注意,计算程序中发生了什么的最基本技术之一是打印程序刚刚读取的内容,这样您就知道程序看到了什么(您可以将其与您期望程序看到的内容进行比较)。这通常会让你走上正确的道路。有没有什么东西像这样对待它呢。因为我要为我的作业做的最终结果应该是这样的。(我把它放在代码的底部)
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