C中的分段错误错误代码在朋友的计算机上运行,​​但不在我的计算机上

时间:2018-04-27 23:24:24

标签: c

我试图为我朋友的计算机上正常运行的项目运行一些代码,但是当我尝试运行它时,我遇到了分段错误错误。

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

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&#34;%d&#34;)atoc99_fscanf.c:30
30 isoc99_fscanf.c:没有这样的文件或目录。

我之前从未使用过调试器,但我不知道这究竟是什么意思所以任何澄清都会很好。

一切都应该正常工作,因为我的朋友能够编译和运行没有问题。除了&#34;分段错误&#34;我没有得到任何类型的错误。当我尝试使用&#34; ./ a.out&#34;在终端中运行代码时用&#34; gcc employee_list.c libel.c&#34;编译后所以我不确定究竟是什么问题。我使用VMware工作站和虚拟薄荷Linux系统使用默认终端。我们都使用相同的设置和编译方式与上面列出的相同。

我是一个完整的编程初学者,所以对此有任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

可能是文件的格式&#34; directory.txt&#34;两者之间并不完全相同。

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

  

编程接收信号SIGSEGV,分段故障。   isoc99_fscanf.c上的__isoc99_fscanf( stream = 0x0 ,格式= 0x401fa2&#34;%d&#34;):30   30 isoc99_fscanf.c:没有这样的文件或目录。

此外,这绝不安全:

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);

我希望它有所帮助!