1

I've been working on the pset6 and implemented the parse and lookup function. I just did something with load function but I am not really much sure if it's correct totally. I haven't implemented the indexes function just yet. Am I going the correct path with the load function?

bool load(FILE* file, BYTE** content, size_t* length)
{
    //integer variable for the for loop
    int i = 0;
    //initiallizing the lenght to 0
    *length = 0;
    //Checking if the file pointer passed to load is NULL.
    if (file == NULL)
    {
        return false;
    }
    //Buffer to store every character from 'file'.
    char* buffer = malloc(sizeof(BYTE));
    if (buffer == NULL)
    {
        return false;
    }

    for (char c = fgetc(file); c != EOF; c = fgetc(file))
    {
        buffer[i] = c;
        buffer = realloc(buffer, sizeof(char) + i);
        *length = *length + 1;
        i++;
    }

    *content = &buffer[0];

    return true;
}

1 Answer 1

2

The biggest problem is that you need to null terminate *content before returning the function. I think it would be better to do this with buffer so you maintain your "*content = &buffer[0]" statement.

Your realloc statement also looks questionable, but it might actually be working. sizeof is included because it makes code more portable. You should use sizeof(char)*i but i is initially zero so this is going to cause problems.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .