Use SEEK_SET instead of literal 0 in lseek calls.