Tomasz
Tomasz

Reputation: 313

Read 300MB binary file to char array

I'm trying to load binary file into char array. My code:

int MAX_FILE_SIZE = 1 000 000
FILE *f;
char buffer[MAX_FILE_SIZE];
f = fopen("sample.bin", "rb"); //sample.bin is 300MB binary file
if (f)
  n = fread(buffer, sizeof(char), MAX_FILE_SIZE, f);

It works until I set MAX_FILE_SIZE to anything bigger than 1M because I recieve program.exe has stopped working. If I thinking right to load all sample.bin to memory I should set MAX_FILE_SIZE to ~300M. How can I do that?

Upvotes: 2

Views: 155

Answers (2)

Leon Carlo Valencia
Leon Carlo Valencia

Reputation: 8757

Depending on your platform and compiler, some limits are set on allowable allocations on stack (that's where your buffer goes). If you want to load the entire file into memory, you would have to use the heap (use malloc). So instead of:

char buffer[MAX_FILE_SIZE];

use:

char *buffer;
if ((buffer = malloc(sizeof(char) * MAX_FILE_SIZE)) == 0) {
    // exit or some other action to take as your OS failed to allocate
}

Upvotes: 2

chqrlie
chqrlie

Reputation: 144979

There is no C syntax for defining large numbers with separators for blocks of digits: int MAX_FILE_SIZE = 1 000 000 should be written int MAX_FILE_SIZE = 1000000;.

Allocating a large array as a local object with automatic storage may cause undefined behavior. The total space available for this depends on the system, but may be less than one megabyte. I suggest you allocate the buffer with malloc() and free it after use:

size_t MAX_FILE_SIZE = 300000000;  // 300MB

int read_file(void) {
    FILE *f;
    int n = -1;
    char *buffer = malloc(MAX_FILE_SIZE);
    if (buffer == NULL)
        return -1;
    f = fopen("sample.bin", "rb"); //sample.bin is 300MB binary file
    if (f) {
        n = fread(buffer, sizeof(char), MAX_FILE_SIZE, f);
        // perform what ever task you want
        fclose(f);
    }
    free(buffer);
    return n;
}

Upvotes: 4

Related Questions