C 将结构的数据分配给数组,然后写入.txt文件时发生总线错误
因此,对于我为类分配的任务,我们应该提示用户输入一些雇员,比如说5,这将提示他们输入每个雇员姓名、工资和部门的数据,然后在数组中存储指向雇员数据的指针。输入完成后,程序应打印数据并将其写入文本文件。所以出于某种原因,当我运行函数时,./employees,我得到了总线错误。有什么线索吗?如果你需要我澄清什么,请告诉我。代码如下:C 将结构的数据分配给数组,然后写入.txt文件时发生总线错误,c,arrays,C,Arrays,因此,对于我为类分配的任务,我们应该提示用户输入一些雇员,比如说5,这将提示他们输入每个雇员姓名、工资和部门的数据,然后在数组中存储指向雇员数据的指针。输入完成后,程序应打印数据并将其写入文本文件。所以出于某种原因,当我运行函数时,./employees,我得到了总线错误。有什么线索吗?如果你需要我澄清什么,请告诉我。代码如下: #include <stdio.h> #include <string.h> #include <stdlib.h> #inclu
#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]);
}
}
我从以下几个方面看到了问题: 在freeMemory中,您试图释放未使用适当函数malloc、calloc或realloc分配的内存 malloc曾经用于为struct _Employee分配内存,但存储在数组中的指针freemory参数显然不是指向_Employee的指针 此外,使用免费as
free(&array[i]);
看起来本身是错误的-您得到的元素地址可能不是第一个-即使数组的地址是从malloc N*sizeofchar调用free&array[i]接收到的,而i>0会导致内存管理错误freerary可以是,并且只有在之前已分配内存而未取消分配内存的情况下,发送到free的地址必须与从malloc接收到的地址相同
编辑:
唯一的解决方案是重新设计程序,或者更确切地说,在编码之前设计程序。祝你好运 在outputData中,为fprintf提供指针:
fprintf(outfile, "%s\n %s\n %s\n", name,
empSalary, department);
编辑:哦!这里和那里似乎有更多的错误:
在freeMemory中:sizeofarray是指针4或8的大小,而不是数组容量。
调用freeMemoryemployeeArray而不是freeMemory*employeeArray
您无法释放堆栈指针上的静态:name、empalary和depatment不是动态的。然而,emp的属性是。
更好的是,删除无用的freeMemory功能。
不幸的结构混合结构和虚假的二维字符串数组。
很多其他的东西。。。
作为一个很好的锻炼和建议!:从代码中删除employeeArray,它会变得更清晰
我还将删除emp的malloc以坚持静态结构和
正确释放emp.name和emp.department在使用后,重新启动前