This is a part of my program:
while((total_bytes_read != fsize) && ((nread = read(f_sockd, filebuffer, fsize_tmp)) > 0)){
if(write(fd, filebuffer, nread) != nread){
perror("write RETR");
onexit(f_sockd, 0, 0, 1);
}
total_bytes_read += nread;
fsize_tmp -= nread;
}
where total_bytes_read, fsize, nread are declared as uint32_t.
On a 64 bit machine there’s no problem, it compiles and run very well (this part of code has to receive a file).
The problem is when i compile on a 32bit machine because i got this error: warning: comparison of integers of different signs: 'ssize_t' (aka 'int') and 'uint32_t' (aka 'unsigned int') [-Wsign-compare] if(write(fd, filebuffer, nread) != nread){
I don’t know how to solve this problem because if i change if(write(fd, filebuffer, nread) != nread){ to if(nread != (uint32_t)write(fd, filebuffer, nread)){ the program doesn’t receive the file anymore but it print (part of) the file content to stdout.
Why i got this strange behaviour and how can i solve this thing?
Just use the right types. The standard says:
So that’s what you should use. Not
int, notuint64_t, notunsigned long longetc.