fread and fwrite seem to only work when using text files and not with jpgs
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Last edited by suicidaleggroll; 02-02-2015 at 01:43 PM.
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 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.
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:
Originally Posted by retroCheck
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
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]
Also noted you are using strlen for the size. Wrong for binaries. You need the absolute size using stat, not the text calculated size. Try man stat as well.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.