first time posting here, Googled for a few hours and I couldn’t find anything.
I am trying to write a client program that reads different files and sends them over the web to a server program. (For backup purposes.)
So far I have used fread() to put the files into a buffer, in a way that is similar to this:
http://www.cplusplus.com/reference/clibrary/cstdio/fread/
It works fine for plain-text files, but when I try to transfer executable files, fread() doesn’t seem to read the files, only reading “ELF” into the buffer.
My question is, what is the proper way to read the contents of an ELF file into a buffer in C?
Thank you.
The proper way to read binary files is simply to use
fread(). But remember that you cannot treat the contents as a string, which means (among other things) you cannot usestrlen()to determine their length.Instead, you must keep the length in a seperate variable. The return value of
fread()will tell you how many records it successfully read – if you set the record size to 1 (the second parameter tofread()) then it will return the number of bytes read.