I think it may have to do with your if statement being > 0.
Here's what the read
man page says (type man 2 read in the terminal):
RETURN VALUE
On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if
this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because
we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal. On error, -1 is
returned, and errno is set appropriately. In this case it is left unspecified whether the file position (if any) changes.
So your code should read something like
if(-1 == read(input,buffer_header,4)) {
perror("error with read");
} else {
do something;
}
Edit: Sorry, just saw the comment thanks Barmar!!
Edit2: Also you should error check the open
syscall similarly.
int input = open(argv[1], O_RDONLY|O_DIRECT);
if(-1 == input) {
perror("error with open");
} else {
do stuff;
}
Here's a short tutorial that may help