gpt4 book ai didi

c - 将结构体数据分配到数组然后写入 .txt 文件时出现总线错误

转载 作者:行者123 更新时间:2023-11-30 17:20:32 25 4
gpt4 key购买 nike

因此,对于我的类作业,我们应该提示用户输入员工数量(例如 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]);
}
}

最佳答案

我发现问题如下:

freeMemory 中,您尝试释放未使用适当函数(malloccallocrealloc 分配的内存) >).

malloc使用了一次为struct _Employee分配内存,但指针存储在array中(freeMemory的参数>) 显然不是指向 _Employee

此外,使用免费作为

 free(&array[i]);

看起来本身是错误的 - 你得到了元素的地址(可能不是第一个 - 即使 array 具有从诸如 malloc( N * sizeof(char)) 之类的东西接收到的地址 code> 调用 free(&array[i]) while i > 0 会导致内存管理错误(free(array) 可能是这样,并且仅当内存已分配且之前未解除分配,并且发送到 free 的地址必须与从 malloc 接收的地址相同)。

编辑:

唯一的解决方案可以提供帮助 - 重新设计程序(或者更好地说,在编码之前设计程序)。祝你好运!

关于c - 将结构体数据分配到数组然后写入 .txt 文件时出现总线错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28555231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com