Use the gdb
debugger, or strace(1), on your executable, to be compiled with all warnings and debug info : gcc -Wall -Wextra -g
with GCC. Read carefully the documentation of read(2), and of every function you are using (including malloc(3), whose failure you forgot to test).
You need to use the result (actually read byte count) of read(2). And you need to handle the error case (when read
gives -1) specially.
What is probably happenning, with a long enough file, is that on the first loop you are reading 1 byte, on the second loop you are reading 2 bytes, on the third loop you have read 3 bytes, etc... (and you forgot to compute 1+2+3 in that case).
You should cumulate and sum all the read_output
and you should handle the case when read(2) gives less than the size
(this should happen the last time your read
gave non zero).
I would instead suggest using a fixed buffer (of constant or fixed size), and repeatedly do a read(2) but carefully using the returned byte count (also, handle errors, and EOF condition).
Be aware that system calls (listed in syscalls(2)) are quite expensive. As a rule of thumb, you should read(2) or write(2) a buffer of several kilobytes (and handle carefully the returned byte count, also testing it against errors, see errno(3)). A program read
-ing only a few bytes at once each time is inefficient.
Also, malloc
(or realloc
) is quite expensive. Incrementing the heap allocated size by one is ugly (since you call malloc
on every loop; in your case you don't even need to use malloc
). You'll better use some geometric progression, perhaps newsize = 4*oldsize/3 + 10;
(or similar).