0

I need help understanding how to do load. I'm not asking to just be given the answer, mind you, just some pointers and hints.

This is what I have so far:

bool load(FILE* file, BYTE** content, size_t* length)
{
    int c;
    while ((c = fgetc(file)) != EOF)
    {
        BYTE *bytes_read = realloc(content, (*length + 1));
        if (bytes_read == NULL)
        {
            exit(1);
        }
        length++;
    }
    return false;
}

Since the spec says to read as many bytes as possible from the file, I thought I'd use a while loop and keep reading until EOF, and to use realloc to continue allocating memory with it as needed (because of the clause "stores those bytes contiguously in dynamically allocated memory on the heap"). I do length++ so that I can try to update length on each iteration of the loop.

If my idea is wrong, please let me know.

2
  • I'll ask this again in another thread. I need to do an update to the code. Sorry for this. – Osman Zakir Nov 30 '16 at 21:55
  • Read comment above. That'll tell you all you need to know. But yeah, this should be closed now. – Osman Zakir Dec 2 '16 at 21:44

Browse other questions tagged .