fread and fwrite seem to only work when using text files and not with jpgs
Using fopen,fwrite doesn't seem to work with jpegs.
fedex.jpg and mypic.jpg should be the same size - -rw-rw-rw- 1 student student 29171 Jan 5 19:09 fedex.jpg -rw-rw-r-- 1 student student 4 Feb 2 11:04 mypic.jpg Code:
|
jpg is binary, yet you're printing it with %s and getting the length for the write with strlen. Also, you're opening the source file with "a" which means append, and opening the destination file with "r" which means read.
You can't use %s and strlen for blocks of binary. All it takes is a null byte in the data block and everything stops (or worse yet, no null byte and you blow through the end of your array and out into undefined memory). Swap your a/r in the opens (assuming you do want to append, which you probably don't...use "w" to overwrite), print it with %x for hex, and use the output of your fread to get the number of bytes read to use in your print and write. |
Also, why use global variables for fp, fp2 and readF? fp and fp2 should be local automatics. A large allocation like readF should come from the heap using new to avoid the C++ global initialization code.
|
printf and strlen aren't used in this context. Use fwrite(stdout) and the return value of the previous fread. (Generally, return values aren't meant to be ignored.)
|
You should read 'I/O on Streams' topic from info page of libc,
Code:
info libc |
You should be using fread() to read a chunk of the file into a buffer, then fwrite() to another file from that buffer, and doing the whole thing in a loop that repeats until there are no more bytes to be read. The information is binary, not null-terminated strings.
|
You don't need the printf(), instead get the return from the fread() to see how many bytes you read, then use readF for the fwrite and the return value from the fread as the size. Omit the printf() statement entirely.
|
Indeed. "printf()" with the "%s" format-specifier will interpret the variable as being a zero-terminated string, which it emphatically is not.
You are moving bytes around. You're not looking at those bytes. |
You have to fclose to finish the write. + means to create and trucate if non existent. Useless on reads. R is read, w is is write. B is binary and not always relevant. Try man 2 fread for more info about the fio functions.
[/B] Quote:
|
Quote:
|
Note: "a+" means append and read, "r+" means read and write.
|
All times are GMT -5. The time now is 05:56 AM. |