1

Me again, still in pset6, right now working on "load", I did this so far:

bool load(FILE* file, BYTE** content, size_t* length)
{ 
    fseek(file, 0, SEEK_END);
    long size = ftell(file);
    fseek(file, 0, SEEK_SET);
    *content = malloc(size);
    if (fread(*content, size, 1, file) == 0)
    {
        return false;
    }
    *length = size;
    return true;
}

and works fine with every file, except with .php files, when the client asks for a php "size" gets a value of -1 and I don't understand why, any help? Thanks guys!

2
  • Same problem. Have you found a solution to this?
    – Lipsy
    Jan 24 '16 at 20:41
  • I kind of found a solution, like @JYsuo said, you can't use fseek() in a pipe, because you don't actually have a file, just a stream of data, what I did instead was to read the data like it was an array: while (fread(&(*content)[i], z, 1, file) == 1) and each time checking if EOF has been reached. I'm not posting this as an answer because I'm still having some issues with my code, so I don't know if this is 100% correct. Jan 29 '16 at 9:08
4

You want function load() to work for both:

  • files that have been opened by fopen()
  • pipes that have been opened by popen()

I also did not manage to make it work by using fseek

I solved the problem by using fgetc(), reading all bytes one by one and at the same time counting them so that I could determine *length too in the end. The more bytes you read the more you realloc() the *content

Be careful of off-by-one errors.

0

I had the exact problem as you did. Then I learned that you can not use fseek() in a pipe, as pipe only allows one-way data flow (for more info, check out https://stackoverflow.com/questions/19187131/how-to-fseek-on-a-pipe).

I solved this problem by malloc() a rather large array, then fread() file into it. It might not be the best method, but I can not think of another way at the moment.

1
  • Doesn't this lead to random stuff being added to your browser output?
    – Lipsy
    Jan 25 '16 at 13:24

You must log in to answer this question.

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