LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-02-2015, 01:30 PM   #1
retroCheck
Member
 
Registered: Jan 2015
Posts: 34

Rep: Reputation: Disabled
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:
  
  1 #include <sys/stat.h>
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <time.h>
  5 #include <errno.h>
  6 #include <iostream>
  7 #include <string.h>
  8 #include <utime.h>
  9 
 10 using namespace std;
 11 FILE *fp;
 12 FILE *fp2;
 13 char readF[2000000];
 14 int main(int argc, char **argv){
 15         fp = fopen(argv[1],"ab+");
 16         fread(readF,2000000,1,fp);
 17         printf("%s",readF);
 18         fp2 = fopen(argv[2],"rb+");
 19         fwrite(&readF,strlen(readF),1,fp2);
 20 }

Last edited by retroCheck; 02-02-2015 at 01:31 PM.
 
Old 02-02-2015, 01:37 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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.
 
Old 02-02-2015, 02:37 PM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
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.
 
Old 02-03-2015, 12:44 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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.)
 
Old 02-03-2015, 07:12 AM   #5
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
You should read 'I/O on Streams' topic from info page of libc,

Code:
info libc
 
Old 02-03-2015, 08:56 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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.
 
Old 02-03-2015, 12:14 PM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
Old 02-03-2015, 12:40 PM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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.
 
Old 02-04-2015, 01:14 PM   #9
multiplex22
Member
 
Registered: Dec 2014
Location: ny, us
Distribution: most
Posts: 56
Blog Entries: 1

Rep: Reputation: Disabled
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 View Post
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:
  
  1 #include <sys/stat.h>
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <time.h>
  5 #include <errno.h>
  6 #include <iostream>
  7 #include <string.h>
  8 #include <utime.h>
  9 
 10 using namespace std;
 11 FILE *fp;
 12 FILE *fp2;
 13 char readF[2000000];
 14 int main(int argc, char **argv){
 15         fp = fopen(argv[1],"ab+");
 16         fread(readF,2000000,1,fp);
 17         printf("%s",readF);
 18         fp2 = fopen(argv[2],"rb+");
 19         fwrite(&readF,strlen(readF),1,fp2);
 20 }
 
Old 02-04-2015, 01:17 PM   #10
multiplex22
Member
 
Registered: Dec 2014
Location: ny, us
Distribution: most
Posts: 56
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by multiplex22 View Post
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.
 
Old 02-05-2015, 05:23 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Note: "a+" means append and read, "r+" means read and write.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
fread fwrite problem in C program, segmentation fault ron7000 Programming 2 10-06-2014 12:19 PM
fread and fwrite errors with mapped memory justaguy_75ae Programming 2 09-08-2009 01:44 PM
best way of using fread and fwrite rohanak Programming 1 05-02-2008 07:26 AM
C programming fread and fwrite using structures exvor Programming 4 09-26-2006 08:56 AM
Merge JPGs mddolloff Linux - Software 5 11-21-2004 07:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration