gpt4 book ai didi

c - C 中的段错误错误。代码在 friend 的计算机上运行,​​但不在我的计算机上运行

转载 作者:行者123 更新时间:2023-11-30 14:49:01 25 4
gpt4 key购买 nike

我正在尝试为一个项目运行一些代码,该代码在我 friend 的计算机上运行良好,但是当我尝试运行它时,我收到了段错误错误。

这是为我们提供的主文件:

EMPLOYEE_LIST.C

#include "libel.h"
#include <stdio.h>
#include <string.h>

int main() {
const char *filename = "directory.txt";
char *csv_prefix = "csv_list";
char find_name[25];
pi person;
el emp_list;
emp_list.num_people = 0;
load_el(&emp_list, filename);
printf("%d employees loaded.\n", emp_list.num_people);
char cont = 'x', // initialize continue to something other than 'Y' or 'N'
tmp; //
int state = -1, correct_search_val;
char srch_critera[25] = "init";
while (state != 4) {
while (state < 1 || state > 4) {
printf("1:\tAdd employee and salary\n");
printf("2:\tSearch directory by first name\n");
printf("3:\tGenerate CSV\n");
printf("4:\tSave and exit program\n");
printf("Enter an option (1-4): ");
scanf("%d", &state);
if (state < 1 || state > 4) {
printf("\nError: number not in range\n\n");
}
}

switch (state) {
// add employee and salary
case 1:
// add one person
printf("Enter a first name: ");
scanf("%s", person.first);
printf("Enter %s's last name: ", person.first);
scanf("%s", person.last);
printf("Enter %s's occupation: ", person.first);
scanf("%s", person.position);
printf("Enter %s's Salary: ", person.first);
scanf("%lf", &person.salary);
scanf("%c", &tmp);
printf("Employee added.\n");
add_person(&emp_list, person);
// determine to add more people to the employee list
while (cont != 'N' && emp_list.num_people < MAXPPL) {
cont = 'x';
while (cont != 'Y' && cont != 'N') {
printf("Would you like to enter another name (Y/N): ");
scanf("%c", &cont);
if (cont != 'Y' && cont != 'N') {
printf(
"Error: User entered '%c'. Must enter either 'Y' "
"or 'N'\n",
cont);
}
scanf("%c", &tmp);
}
if (cont != 'N') {
printf("Enter a first name: ");
scanf("%s", person.first);
printf("Enter %s's last name: ", person.first);
scanf("%s", person.last);
printf("Enter %s's occupation: ", person.first);
scanf("%s", person.position);
printf("Enter %s's Salary: ", person.first);
scanf("%lf", &person.salary);
scanf("%c", &tmp);
printf("Employee added.\n");
add_person(&emp_list, person);
}
}
printf("\nReturning to main menu...\n\n");
state = -1;
break;

// search directory by first name
case 2:

cont = 'x'; // reset continue to neither 'Y' nor 'N'
while (cont != 'N') {
cont = 'x';
printf("Enter a person's name to search for: ");
scanf("%s", find_name);
scanf("%c", &tmp);
search_el(emp_list, find_name);
while (cont != 'Y' && cont != 'N') {
printf("\nContinue (Y/N)? ");

scanf("%c", &cont);
fflush(stdout); //, &tmp);
scanf("%c", &tmp);
if (cont != 'Y' && cont != 'N') {
printf(
"Error: User entered '%c'. Must enter either 'Y' "
"or 'N'.\n",
cont);
}
}
}
printf("\nReturning to main menu...\n\n");
state = -1;
break;

// generate CSV file
case 3:
correct_search_val = -1;
while (correct_search_val != 0) {
printf("Generate CSV based on? (\"Salary\", \"Position\"): ");
scanf("%s", srch_critera);
if (!strcmp(srch_critera, "Salary")) {
printf("Generating CSV based on salary...\n");
gen_csv_sal(&emp_list);
correct_search_val = 0;
} else if (!strcmp(srch_critera, "Position")) {
printf("Generating CSV based on position...\n");
gen_csv_pos(&emp_list);
correct_search_val = 0;
} else
printf("Options are: \"Salary\", \"Position\"\n");
}
printf("Returning to main menu...\n\n");
state = -1;

case 4:
break;
} // end switch
} // end while

// save the employee list
save_el(&emp_list, filename);
printf("%d employees saved.\n", emp_list.num_people);
return 0;
} // end main

这些是我们必须创建的文件,以便向上述代码添加功能:

LIBEL.C

#include "libel.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// ADD FUNCTION DEFINITIONS FOR LOAD_EL, SAVE_EL, ADD_PERSON, AND SERACH_EL HERE
void load_el(el *emp_list, const char *filename) {
FILE *ifp;
ifp = fopen(filename, "r");
fscanf(ifp, "%d", &emp_list->num_people);

for (int i = 0; i < emp_list->num_people; i++) {
fscanf(
ifp,
"%s %s %s %lf",
emp_list->people[i].first,
emp_list->people[i].last,
emp_list->people[i].position,
&emp_list->people[i].salary);
}
fclose(ifp);
return;
}

void add_person(el *emp_list, pi person) {
emp_list->people[emp_list->num_people] = person;
emp_list->num_people++;
return;
}

void search_el(el emp_list, char find_name[]) {
int count = 0;
for (int i = 0; i < emp_list.num_people; i++) {
if (strcmp(emp_list.people[i].first, find_name) == 0) {
printf(
"\nName: %s %s\nPosition: %s\nSalary: %lf\n",
emp_list.people[i].first,
emp_list.people[i].last,
emp_list.people[i].position,
emp_list.people[i].salary);
count++;
}
}
if (count == 0) {
printf("No entries with that name.\n");
}

return;
}

LIBEL.H

#ifndef _LIBCL_H_
#define _LIBCL_H_
#define MAXPPL 500
#define MAXLEN 25


struct personal_info {
char first[MAXLEN];
char last[MAXLEN];
char position[MAXLEN];
double salary;
};

typedef struct personal_info pi;


struct employee_list {
pi people[MAXPPL];
int num_people;
};

typedef struct employee_list el;


//ADD PROTOTYPES HERE
void load_el(el * emp_list, const char * filename);
void add_person(el * emp_list, pi person);
void search_el(el emp_list, char find_name[ ]);
void save_el(el * emp_list, const char * filename);

void gen_csv_sal(el * emp_list);
void gen_csv_pos(el * emp_list);
char * gen_file_name(char * filename, int filename_size, char * suffix, int suffix_size);
#endif

我通过调试器运行我的代码,并收到以下错误:

程序收到信号SIGSEGV,段错误。
__isoc99_fscanf(流=0x0,格式=0x401fa2“%d”)位于 isoc99_fscanf.c:30
30 isoc99_fscanf.c:没有这样的文件或目录。

我以前从未使用过调试器,并且我不知道这到底意味着什么,所以任何澄清都会很好。

一切都应该工作正常,因为我的 friend 能够毫无问题地编译和运行。当我在使用“gcc employee_list.c libel.c”编译后尝试使用“./a.out”在终端中运行代码时,除了“段错误”之外,我没有收到任何类型的错误,所以我没有确定到底是什么问题。我正在使用 VMware 工作站和虚拟 Mint Linux 系统,并使用默认终端。我们都使用相同的设置并以与上面列出的相同的方式进行编译。

我是一个完全的编程初学者,因此我们将不胜感激。

最佳答案

也许你们俩的“directory.txt”文件的格式不完全相同。

看起来fscanf的输入是0(NULL):

Program received signal SIGSEGV, Segmentation fault. __isoc99_fscanf (stream=0x0, format=0x401fa2 "%d") at isoc99_fscanf.c:30 30 isoc99_fscanf.c: No such file or directory.

此外,这永远不安全:

ifp = fopen(filename, "r");
fscanf(ifp, "%d", &emp_list->num_people);

尝试如下:

void load_el(el *emp_list, const char *filename) {
FILE *ifp;
ifp = fopen(filename, "r");
if(0 == ifp) {
printf("File not found!\n");
return;
}
fscanf(ifp, "%d", &emp_list->num_people);

希望对您有帮助!

关于c - C 中的段错误错误。代码在 friend 的计算机上运行,​​但不在我的计算机上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50071399/

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