【发布时间】:2015-02-17 05:24:33
【问题描述】:
所以对于我的课堂作业,我们应该提示用户输入员工的数量(比如 5 个),然后提示他们输入每个员工的数据(姓名、薪水和部门),然后将指向员工数据的指针存储在数组中。输入完成后,程序应打印数据并将其写入文本文件。所以由于某种原因,当我运行函数 ./employees 时,我得到“总线错误”。任何线索为什么?如果您需要我澄清任何事情,请告诉我。代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "struct.h"
#include "employees.h"
#include <readline/readline.h>
#include <readline/history.h>
#include <errno.h>
#define MAXLINE (100) // MAXLINE is maximum length of an input line.
int main() {
char* inputline; // pointer to input line
char name[MAXLINE];
int salary;
char empSalary[MAXLINE];
char department[MAXLINE];
int num; // Number of employees
int i; // Loop counter
char* employeeArray[num*3];
FILE *outfile = fopen("employee.txt", "w"); // Open or create file for writing
inputline = readline("Enter the total number of employees: "); // Read the input line.
num = atoi(inputline);
for (i = 1; i < num + 1; i++) {
struct _Employee* emp = malloc(sizeof(struct _Employee));
inputline = readline("Enter the name of the employee: "); // Read the input line
sscanf(inputline, "%s", name); // reads formatted input from a string.
inputline = readline("Enter the salary of the employee: "); // Read the input line
if (sscanf(inputline, "%s", salary) == 1) {
printf("Invalid salary. Try again!\n"); // Error message for if the salary is less than 0
inputline = readline("Enter the salary of the employee: "); // Read the input line
salary = atoi(inputline); // Converts inputline to an integer and sets salary to that value.
}
salary = atoi(inputline); // Converts inputline to an integer and sets salary to that value.
if (salary < 0) {
printf("Invalid salary. Try again!\n"); // Error message for if the salary is less than 0
inputline = readline("Enter the salary of the employee: "); // Read the input line
salary = atoi(inputline); // Converts inputline to an integer and sets salary to that value.
}
inputline = readline("Enter the department of the employee: "); // Read the input line
sscanf(inputline, "%s", department); // reads formatted input from a string
emp->name = strdup(name);
emp->salary = salary;
emp->department = strdup(department);
sprintf(empSalary,"%d", salary);
employeeArray[(i*3) - 3] = name;
employeeArray[(i*3) - 2] = empSalary;
employeeArray[(i*3) - 1] = department;
printEmployee(emp);
printf("About to write to file.\n");
if (outfile == NULL) {
printf("[employee] %d The open was unsuccessful!\n", errno, strerror(errno));
return 1;
}
else {
outputData(outfile, name, empSalary, department);
}
}
fclose(outfile);
freeMemory(*employeeArray);
}
/* Writes the input data to a text file employee.txt
* @param stream The file to be written to
* @param name Name of the employee
* @param empSalary Salary of the employee
* @param department Department the employee works in
*/
void outputData(FILE *stream, char* name, char* empSalary, char* department) {
FILE *outfile = fopen("employee.txt", "w"); // Open or create file for writing
fprintf(outfile, "%s\n %s\n %s\n", *name, *empSalary, *department);
}
/* Frees the memory of the array
* @param array The array to be de-allocated
*/
void freeMemory(char* array) {
int i; // Loop counter
for (i = 0; i < sizeof(array); i++) {
free(&array[i]);
}
}
【问题讨论】: