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 10-02-2004, 12:56 AM   #1
sagar13
LQ Newbie
 
Registered: Oct 2004
Location: houston
Posts: 1

Rep: Reputation: 0
binary files in C


Hi,
I want to read a binary file (say *.jpg ) in C and rewrite it to some other new file.
How can I do that ?
 
Old 10-02-2004, 02:15 AM   #2
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
You can use the standard C stdio file stream to do this. Make sure you open the file in binary mode.
 
Old 10-02-2004, 06:31 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Note: Opening a file in "binary" mode or in in "text mode" does not make any difference in UNIX/Linux. It has effect only on DOS/Windows (and MacOS?)
 
Old 10-02-2004, 06:34 AM   #4
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Thanks for point that out
 
Old 10-02-2004, 09:35 AM   #5
onnyloh
Member
 
Registered: Nov 2002
Posts: 57

Rep: Reputation: 15
are u sure?

Quote:
Originally posted by Hko
Note: Opening a file in "binary" mode or in in "text mode" does not make any difference in UNIX/Linux. It has effect only on DOS/Windows (and MacOS?)
 
Old 10-02-2004, 10:01 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by onnyloh
are u sure?
As I indicated, I'm not sure about Mac-OS.
For the rest: Yes, I'm definitely sure.
 
Old 10-02-2004, 10:04 AM   #7
mirradric
Member
 
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188

Rep: Reputation: 31
Yes. The main reason why we need to use text/binary modes is that windows/dos treats a line break as 2 characters. ( Can't rem which 2 though, never bother to remember). When a file is in text mode, the windows io calls will convert instances of the \n character into it's 2 components and vice versa. It does not do this translation if the file is in binary mode. On *nix, a line break is just a single character; so there is no need for such translation at all. There is no distinction of a text file and a binary file on a *nix. (other than the kinds of ..err.. bytes you can find in them)
 
Old 10-02-2004, 10:13 AM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
It may still be a good idea to specify mode "b" with fopen(), just in case the program is going to be ported to Windows/DOS one day...
 
Old 10-02-2004, 10:14 AM   #9
mirradric
Member
 
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188

Rep: Reputation: 31
If I didn't remember wrongy, pre-OS X macs uses the other character. I suspect OS X should follow the *nix way
 
Old 10-02-2004, 11:12 AM   #10
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Directly from 'man fopen':
Quote:
The mode string can also include the letter ``b'' either
as a last character or as a character between the charac-
ters in any of the two-character strings described above.
This is strictly for compatibility with ANSI X3.159-1989
(``ANSI C'') and has no effect; the ``b'' is ignored on
all POSIX conforming systems, including Linux. (Other
systems may treat text files and binary files differently,
and adding the ``b'' may be a good idea if you do I/O to a
binary file and expect that your program may be ported to
non-Unix environments.)
 
Old 10-02-2004, 01:29 PM   #11
onnyloh
Member
 
Registered: Nov 2002
Posts: 57

Rep: Reputation: 15
sigh, I put 'b' in fopen for a binary I/O all the ways.
Never know that this is useless...
:S
 
  


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
Binary Files? Teralon 9 Linux - General 2 02-10-2004 10:36 PM
Binary files sodaforce Linux - Software 1 09-07-2003 05:31 PM
Using binary files KDE4me Linux - Newbie 1 04-24-2003 08:00 AM
Binary Files guywithredhat Linux - General 7 03-18-2003 06:28 PM
binary files walterw Programming 7 01-27-2003 08:27 AM

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

All times are GMT -5. The time now is 06:13 AM.

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