Quote:
I also know that '\0' is a null terminator, an equivalent of 0, an int. Like a condition that uses EOF, it returns '\0' or 0, upon EOF. Which cannot be used with fgets. Since fgets returns a pointer to a file, aka an FH, and EOF returns an int. I used it with fread because i thought that the condition i set "meant", that as long as the file does not equal EOF, populate the buffer with the data from the file.
Couple things. I noticed in the code you used "fd" as a suffix on some variable names (like "writefd") for storing the return value of file operations, like write(). But those are not fd's at all, and have nothing to do with them. Just because a function returns an int and operates on files does not make that int a file descriptor, so you should stop labelling the variable that way because it contributes to a significant misunderstanding.
Quote:
Originally Posted by man fread
RETURN VALUE
fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters). If an error occurs, or the end-of-file is reached, the return value is a short item count (or zero).
fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred.
Okay, so that point about the return value of all the various file reading/writing/transmitting functions should be easy to remember:
Quote:
Originally Posted by MK27
a) if the file is not a text file, since fread() may have read data including zero bytes, but strlen() counts them as the null terminator.
b) if the file is a text file, because fread does not add a '\0', and if there is not one in buffer, strlen(buffer) will go out of bounds and possibly cause a seg fault.
Quote:
bytes = fread(buffer, sizeof(buffer), 1, fd);
Whoops! Fread(), as just discussed, returns the number of objects, not the number of bytes. The number of bytes in an object is determined by the second arg. So we could subsequently do this: