홈>
친구의 컴퓨터에서 잘 작동하는 프로젝트에 대해 일부 코드를 실행하려고하는데 실행하려고하면 분할 오류 오류가 발생합니다.
이것은 우리에게 제공된 주요 파일입니다 :
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.c에서 __isoc99_fscanf (스트림 = 0x0, 형식 = 0x401fa2 "% d") : c : 30
30 isoc99_fscanf.c : 그러한 파일이나 디렉토리가 없습니다.
이전에 디버거를 사용해 본 적이 없으며 이것이 무엇을 의미하는지 전혀 알지 못하므로 설명이 좋을 것입니다.
친구가 아무 문제없이 컴파일하고 실행할 수 있으므로 모든 것이 잘 작동해야합니다. "gcc employee_list.c libel.c"로 컴파일 한 후 "./a.out"으로 터미널에서 코드를 실행하려고 할 때 "세그먼트 결함"이외의 다른 종류의 오류가 발생하지 않습니다. 정확히 문제가 무엇인지 확인하십시오. 기본 터미널을 사용하는 가상 민트 Linux 시스템에서 VMware 워크 스테이션을 사용하고 있습니다. 위에 나열된 것과 동일한 방식으로 동일한 설정을 사용하고 컴파일합니다.
나는 프로그래밍에 대한 완전한 초보자이므로 이것에 대한 도움을 주시면 감사하겠습니다.
- 답변 # 1
관련 자료
- Omnet ++ 단순 모듈의 C ++ 코드에서 Python 임베디드 코드의 분할 오류 오류
- 내 C 프로그램에서이 오류의 원인은 무엇입니까 (분할 오류 (코어 덤프))?
- c - 코드를 실행할 때 세분화 오류 (코어 덤프) 오류가 발생합니다
- 배열의 최대 값을 반환하는 함수의 C 분할 오류
- c - 크기에 따른 어레이 배열의 세분화 오류
- arrays - 세분화 오류 (코어 덤프 됨)에 대한 조언 필요
- error handling - C에서 분할 오류를 보여주는 코드를 어떻게 수정할 수 있습니까?
- c++ - 분할 오류가 발생하는 이유는 무엇입니까? 내가 도대체 뭘 잘못하고있는 겁니까?
- c - 왜 이러한 세분화 오류입니까?
- c - pthread를 사용한 분할 오류
- c - /에서 분할 오류
- C 프로그램에서 x86 어셈블리 함수를 호출 할 때 분할 오류
- c++ - 이 기능으로 인해 분할 오류가 발생하는 이유는 무엇입니까?
- c - ELF 바이너리 내부에 삽입 된 코드를 실행하려고 할 때 분할 오류 수신
- c - 이 프로그램에 대해 분할 오류가 발생하는 이유는 무엇입니까?
- C - 씨 - 루프에서 fgets ()로 세분화 오류 얻기
- c - 분할 오류 - 이중 포인터를 사용하여 동적으로 행렬 할당
- string - char 포인터가있는 strsep ()는 c에서 분할 오류를 제공합니다
- c - 구조체 멤버에 값을 추가 할 때 구조체 분할 오류
- c - 어레이 인쇄 후 분할 오류?
트렌드
- OpenCv의 폴더에서 여러 이미지 읽기 (python)
- 파이썬 셀레늄 모든 "href"속성 가져 오기
- html - 자바 스크립트 - 클릭 후 변경 버튼 텍스트 변경
- git commit - 자식 - 로컬 커밋 된 파일에 대한 변경을 취소하는 방법
- JSP에 대한 클래스를 컴파일 할 수 없습니다
- javascript - 현재 URL에서 특정 div 만 새로 고침/새로 고침
- jquery - JavaScript로 현재 세션 값을 얻으시겠습니까?
- javascript - swiperjs에서 정지, 재생 버튼 추가
- JavaScript 변수를 HTML div에 '출력'하는 방법
- python - 문자열에서 특정 문자 제거
"directory.txt"파일의 형식이 둘 다 정확히 동일하지 않을 수도 있습니다.
fscanf의 입력이 0 (NULL) 인 것 같습니다 :
와이즈 비즈또한 안전하지 않습니다 :
다음과 같이 해보십시오 :
도움이 되길 바랍니다!
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);