c - Using fread() hangs until killed -


this general structure of code:

if (contentlength > 0) {     // send post data     size_t sizeread = 0;     char buffer[1024];     while ((sizeread < contentlength) && (!feof(stream)))     {         size_t diff = contentlength - sizeread;         if (diff > 1024)             diff = 1024;          // debuging         fprintf(stderr, "sizeread: %zu\n", sizeread);         fprintf(stderr, "contentlength: %ul\n", contentlength);         fprintf(stderr, "diff: %zu\n", diff);          size_t read = fread(buffer, 1, diff, stream);         sizeread += read;          exit(1);          // write pipe         fwrite(buffer, 1, read, cgipipepost);          exit(1);     } } 

however, program hangs when hits fread() line. if add exit() before line, program exists. if add after, program hangs until send sigint signal.

any appreciated, have been stuck on quite time now.

thanks

fread tries fill internal buffer. depending on implementation, may able stop or limit setting buffering mode (in particular, setting _ionbf, see setbuf, should work implementations). general rule, though, avoid mixing counted i/o on sockets stdio @ all—to use raw read calls.

also, while it's not biting here, !feof(stream) test wrong: people mean predictive (eof occur), feof "post-dictive": after read operation fails (getc or fgetc returns eof), feof , ferror indicators allow discover why previous failure occurred.


Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -