将信息从文件存储到C中的结构中-遇到分段错误

时间:2019-06-16 16:55:30

标签: c file io scanf structure

我正在编写一个程序来存储输入文件中的信息并打印出用户选择的信息。我还没有进入使用选择部分,但是我马上就遇到了分段错误。我知道这意味着我正在尝试访问内存中不存在或无法访问的位置。

我不确定自己在做什么错。我正在尝试将输入文件中的信息存储到我的结构中。

输入文件的格式为

3
5 Name Name 10 56789
7 Name Name 7 67894
8 Name Name 10 89375

我尝试直接以emp [1] .id等(而不是emp [i] .id等)访问结构。这也没有用。

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

// structures
struct emp
{
    int id;
    char firstname[10];
    char lastname[10];
    int department;
    float salary;
} emp[10];


// function prototypes
// nothing here yet

int main(int argc, char *argv[])
{

int i = 0;
int choice;

if(argc != 2){

printf("Usage: %s input.txt\n", argv[0]);
exit(EXIT_FAILURE);
}

FILE* inputFile;


inputFile = fopen("input.txt", "r");

    if(inputFile == NULL){
            printf("Error opening %s\n", argv[1]);
            exit(EXIT_FAILURE);
    }
// file is open now

// loop to save information from file into structure

 int num;

    fscanf(inputFile, "%d", &num);


            for(i = 0; i < num; i++){
    fscanf(inputFile, "%d", emp[i].id);
    fscanf(inputFile, "%s", emp[i].firstname);
    fscanf(inputFile, "%s", emp[i].lastname);
    fscanf(inputFile, "%d", emp[i].department);
    fscanf(inputFile, "%f", emp[i].salary);

}


    printf("\n");
    printf("Welcome to the Employee Database!\n");
    printf("---------------------------------\n");
    printf("Choose an option:\n");
    printf("1:   Print empid\n");
    printf("2:   Print ALL employees\n");
    printf("3:   Show ALL employees in department\n");
    printf("-1:  QUIT\n");
    scanf("%d", &choice);

// I have not set up the functions to perform the selection options yet 
return 0;
}

这是我收到的输出。

c803@cs2:~A5$ gcc A5.c
c803@cs2:~A5$ ./a.out input.txt
Segmentation fault

1 个答案:

答案 0 :(得分:2)

在这里,fscanf像scanf()一样,将变量的内存地址存储到读取的数据中。 您需要在emp [i] .id和其他所有数据成员(字符数组除外)的前面加上“&”,因为数组名称本身会给出数组的第一个数组成员的地址。 所以代码应该是:

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

// structures
struct emp
{
    int id;
    char firstname[10];
    char lastname[10];
    int department;
    float salary;
} emp[10];


// function prototypes
// nothing here yet

int main(int argc, char *argv[])
{

int i = 0;
int choice;

if(argc != 2){

printf("Usage: %s input.txt\n", argv[0]);
exit(EXIT_FAILURE);
}

FILE* inputFile;


inputFile = fopen("input.txt", "r");

    if(inputFile == NULL){
            printf("Error opening %s\n", argv[1]);
            exit(EXIT_FAILURE);
    }
// file is open now

// loop to save information from file into structure

 int num;

    fscanf(inputFile, "%d", &num);


            for(i = 0; i < num; i++){
    fscanf(inputFile, "%d", &emp[i].id);
    fscanf(inputFile, "%s", emp[i].firstname);
    fscanf(inputFile, "%s", emp[i].lastname);
    fscanf(inputFile, "%d", &emp[i].department);
    fscanf(inputFile, "%f", &emp[i].salary);

}


    printf("\n");
    printf("Welcome to the Employee Database!\n");
    printf("---------------------------------\n");
    printf("Choose an option:\n");
    printf("1:   Print empid\n");
    printf("2:   Print ALL employees\n");
    printf("3:   Show ALL employees in department\n");
    printf("-1:  QUIT\n");
    scanf("%d", &choice);

// I have not set up the functions to perform the selection options yet 
return 0;
}