意外的Segfault - 我做错了什么

时间:2014-05-04 23:49:08

标签: c segmentation-fault

我一直在努力让蜘蛛网从我的C编程技能中脱颖而出,而且我一直在犯一个我似乎无法弄清楚的错误。该程序读入由换行符分隔的整数列表。这个位发生在read_integer_file中......我没有遇到任何问题。当我将数据传回主数据库时,我遇到了问题。

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

int read_integer_file(char* filename, int* out)
{
    FILE* file;
    file = fopen(filename, "r");
    /* check if the file open was successful */
    if(file == NULL)
    {
        return 0;
    }

    int num_lines = 0;

    /* first check how many lines there are in the file */
    while(!feof(file))
    {
        fscanf(file, "%i\n");
        num_lines++;
    }

    /* seek to the beginning of the file*/
    rewind(file);

    out = malloc(sizeof(int)*num_lines);

    if(out == NULL)
        return 0;

    int inp = 0;
    int i = 0;
    while(!feof(file))
    {
        fscanf(file, "%i\n", &inp);
        out[i] = inp;
        printf("%i\n", out[i]); /* <---- Prints fine here! */

        i++;
    }

    return num_lines;
}

int main(int argc, char** argv)
{
    if(argc < 2)
    {
        printf("Not enough arguments!");
        return -1;
    }

    /* get the input filename from the command line */
    char* array_filename = argv[1];

    int* numbers = NULL;
    int number_count = read_integer_file(array_filename, numbers);

    for(int i = 0; i < number_count; i++)
    {
        /* Segfault HERE */
        printf("%i\n", numbers[i]);
    }
}

2 个答案:

答案 0 :(得分:3)

您尚未为数字分配任何内存。目前它指的是没有。当它返回到调用函数时,它仍指向无处。将指针传递给指向函数的指针,以便在函数中分配它。

int read_integer_file(char* filename, int** out)
{
     ...
     *out = malloc(sizeof(int)*num_lines);
     ...

     int number_count = read_integer_file(array_filename, &numbers);

答案 1 :(得分:2)

这是你的代码工作的一个版本..请记住,fscanf只是跳过你编写它的方式所以它就像写fscanf(文件,“%d”);

如果你没有放一个变量来处理读取的内容,编译器可能看不到它,但你可能会收到错误..

所以这是代码:

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

int read_integer_file(char* filename, int **out)
{
    FILE* file;
    file = fopen(filename, "r");
    /* check if the file open was successful */
    if(file == NULL)
    {
        return 0;
    }

    int num_lines = 0;
    int garbi;
    char garbc;

    /* first check how many lines there are in the file */
    while(!feof(file))
    {
        fscanf(file, "%d", &garbi);
        fscanf(file, "%c", &garbc);
        if (garbc=='\n') ++num_lines;
    }

    /* seek to the beginning of the file*/
    rewind(file);

    int *nbr = malloc(sizeof(int)*num_lines);

    if(nbr == NULL)
        return 0;

    int i = 0;
    while(!feof(file))
    {
        fscanf(file, "%d", &nbr[i++]);
        fscanf(file, "%c", &garbc);
    }
    *out=nbr;

    return num_lines;
}

int main(int argc, char** argv)
{
    if(argc < 2)
    {
        printf("Not enough arguments!");
        return -1;
    }

    /* get the input filename from the command line */
    char* array_filename = argv[1];

    int *numbers = NULL;
    int number_count = read_integer_file(array_filename, &numbers);

    int i;
    for(i = 0; i < number_count; ++i)
        printf("%d\n", numbers[i]);

    return 0;
}