【问题标题】:Storing information from a file into a structure in C - running into segmentation fault将文件中的信息存储到 C 中的结构中 - 遇到分段错误
【发布时间】:2019-10-30 10:20:55
【问题描述】:

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

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

输入文件是这种格式

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

【问题讨论】:

  • 调试器对崩溃说了什么?
  • 始终启用编译器警告:gcc -Wall -Wextra -pedantic -O2 A5.c
  • 可能num大于10
  • A5.c:60:29: 警告:格式 '%d' 需要类型为 'int *' 的参数,但参数 3 的类型为 'int' [-Wformat=] fscanf(inputFile, " %d", emp[1].id); ^ A5.c:63:29:警告:格式 '%d' 需要类型为 'int *' 的参数,但参数 3 的类型为 'int' [-Wformat=] fscanf(inputFile, "%d", emp[1 ]。部); ^ 我会努力看看我现在是否可以解决这些问题。谢谢。
  • 说真的。我只是忘记了&...谢谢。

标签: c file io scanf structure


【解决方案1】:

这里 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;
}

【讨论】:

  • 是的。一些愚蠢而简单的事情。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2012-08-01
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-22
  • 2020-12-07
  • 1970-01-01
相关资源
最近更新 更多