[Solved] How do I read a large file of numbers and store its content in an array?


fopen, calloc, and realloc can all fail, returning NULL.

In the case of realloc, the original allocation will remain untouched, so immediately overwriting the original pointer with the newly returned value will leak memory and throw away potentially useful data.

Reallocating every iteration is rather costly. A better strategy is to reallocate when the buffer is full, increasing the memory by some factor. Doubling the current size is a simple approach.

Strictly sized buffers like char [6] are prone to filling up too quickly. Be more generous with your input buffer sizes.

The primary issue is that size is never changed during the loop, so the allocated memory does not keep up with the data read. The buffer overflows and you invoke Undefined Behaviour, likely overwriting some data you should not have touched.

Run this program as ./a.out nums.txt, or omit the argument to read from stdin.

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

int main(int argc, char **argv)
{
    char line[128];
    FILE *file = argc > 1 ? fopen(argv[1], "r") : stdin;

    if (!file) {
        perror(argv[1]);
        return EXIT_FAILURE;
    }

    size_t size = 1024;
    size_t length = 0;
    int *list = malloc(sizeof *list * size);

    if (!list) {
        perror("malloc");
        return EXIT_FAILURE;
    }

    while (fgets(line, sizeof line, file)) {
        int value = atoi(line);

        if (!value)
            continue;

        if (length == size) {
            size *= 2;
            void *memory = realloc(list, sizeof *list * size);

            if (!memory) {
                perror("realloc");
                break;
            }

            list = memory;
        }

        list[length++] = value;
    }

    if (argc > 1)
        fclose(file);

    /* arbitrary use of data */
    size_t odds = 0;

    for (size_t i = 0; i < length; i++)
        if (list[i] & 1)
            odds++;

    printf("Number of odd values: %zu\n", odds);

    free(list);
}

1

solved How do I read a large file of numbers and store its content in an array?