繁体   English   中英

意外的段错误-我在做什么错

[英]Unexpected Segfault - What am I doing wrong

我一直在尝试从我的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]);
    }
}

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

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

     int number_count = read_integer_file(array_filename, &numbers);

这也是代码工作的一个版本。还要记住,fscanf只是跳过\\ n编写方式,因此就像编写fscanf(file,“%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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM