質問

I've been trying to blow the cobwebs off my C programming skills, and I've been getting an error I can't seem to figure out. This program reads in a list of integers separated by newlines. This bit happens in read_integer_file... I have no issues going through the input there. It's when I pass the data back to main via out that I have the problem.

#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]);
    }
}
役に立ちましたか?

解決

You have not allocated any memory for numbers. Currently it is pointing to no where. When it gets back to the calling function it is still pointed to nowhere. Pass a pointer to a pointer to the function to allocate it within the function.

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

     int number_count = read_integer_file(array_filename, &numbers);

他のヒント

This is a version of your code working.. Keep in mind also that fscanf just skip the \n the way you wrote it so it's like writing fscanf(file, "%d");

And if you don't put a variable to handle what it reads the compiler may not see it but you'll probably get an error..

So here is the code :

#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帰属
所属していません StackOverflow
scroll top