By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

read and write binary file

P: n/a
Hi ALL,

I want to read a binary file(it's pic.tif file, I guess it's binary
file?), then write it to a new file), I have several questions about
this process:

When I use fread() to read a chunk of the file into a buffer, when it
encounters the end of the file, will the EOF indicator be put into the
buffer automatically just as an ordinary byte of the file, or do I
have to do it manually?

When I want to write chunks received to a new file, do I just write
each chunk sequentially until EOF? Do I need to append EOF to the new
file?

Suppose all chunks have been received and written to the new file,
then if I just name the new file with the correct extension, say
pic2.tif,will I be able to recover the original file like this?

I'm very new to C so your help is much appreciated.

Thanks in advance.

Mar 23 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ericunfuk wrote:
I want to read a binary file(it's pic.tif file, I guess it's binary
file?), then write it to a new file)
In general, all files are binary. There are some OSs that have special
textfiles, where the system transparently translates lineendings. As far
as C is concerned, there is also the distinction between using e.g.
fprintf() which does formatting (as text) before writing and fwrite, which
takes preformatted data.

When I use fread() to read a chunk of the file into a buffer, when it
encounters the end of the file, will the EOF indicator be put into the
buffer automatically just as an ordinary byte of the file, or do I
have to do it manually?
No, the value EOF is only used by e.g. fgetc() to signal that there was no
character to read. fread() signals how much was read directly and doesn't
use this signal value. Anyway, you typically can't store the EOF value in
a byte!
When I want to write chunks received to a new file, do I just write
each chunk sequentially until EOF? Do I need to append EOF to the new
file?
Again, EOF is not an can not be part of the file, therefore you don't write
it.
Suppose all chunks have been received and written to the new file,
then if I just name the new file with the correct extension, say
pic2.tif,will I be able to recover the original file like this?
Whether an extension is required and needed is up to your OS. Many OSs
don't need them to function.
I'm very new to C so your help is much appreciated.
Get a good book, check out the reviews at ACCU's website.

Uli
Mar 23 '07 #2

P: n/a
"ericunfuk" <xu***********@gmail.comwrites:
When I use fread() to read a chunk of the file into a buffer, when it
encounters the end of the file, will the EOF indicator be put into the
buffer automatically just as an ordinary byte of the file, or do I
have to do it manually?
EOF is not a byte in a file and fread will not put it into a
buffer for you. Nor should you put it into the buffer yourself:
when converted to a character type, it will have the same value
as some other byte, so that later code will confuse that value
with an ordinary byte in the file.
When I want to write chunks received to a new file, do I just write
each chunk sequentially until EOF? Do I need to append EOF to the new
file?
EOF is not a byte in a file and you cannot write it to a file.
You can try to write it to a file, but in fact you will just
write an ordinary byte, and thus I do not recommend trying.
Suppose all chunks have been received and written to the new file,
then if I just name the new file with the correct extension, say
pic2.tif,will I be able to recover the original file like this?
You mean: if you copy the contents of one binary file accurately
to another, by using fread and fwrite, will the second file be
identical to the first? Ordinarily, yes: although I believe that
C allows the implementation to append any number of null bytes to
the second file, most implementations don't do that.
--
Comp-sci PhD expected before end of 2007
Seeking industrial or academic position *outside California* in 2008
Mar 23 '07 #3

P: n/a
>
No, the value EOF is only used by e.g. fgetc() to signal that there was no
character to read. fread() signals how much was read directly and doesn't
use this signal value. Anyway, you typically can't store the EOF value in
a byte!
When I want to write chunks received to a new file, do I just write
each chunk sequentially until EOF? Do I need to append EOF to the new
file?

Again, EOF is not an can not be part of the file, therefore you don't write
it.
I am confused.

Then how can I tell if there's no more bytes to read from?using feof()?
I tried a combination of fseek() and feof(), but there is the
possibility that when ´╝ęgive fseek() an offset, it may seek over the
actual end of the file and reset EOF, thus the program never ends
because feof()can't find EOF?

I know I can use fread() to read from the start to the end without
fseek(), but I need to seek back and forth in case packets are lost
during transfer(I'm transferring a file using UDP, the client and
server are on the same machine for testing). Also the length of the
read buffer(or the length of my packet)is fixed, so what if the file
size is not an exact multiple of the buffer size, I'm doomed to seek
over EOF?

Eric
Mar 23 '07 #4

P: n/a
"ericunfuk" <xu***********@gmail.comwrites:
Then how can I tell if there's no more bytes to read from?using feof()?
You just try to do a read. If the read doesn't return as many
bytes as you asked for, either an error occurred or you hit end
of file. You can use ferror() and feof() to figure out which one
happened.

Did you read the FAQ? The following question and answer might be
helpful.

12.2: Why does the code

while(!feof(infp)) {
fgets(buf, MAXLINE, infp);
fputs(buf, outfp);
}

copy the last line twice?

A: In C, end-of-file is only indicated *after* an input routine has
tried to read, and failed. (In other words, C's I/O is not like
Pascal's.) Usually, you should just check the return value of
the input routine (in this case, fgets() will return NULL on end-
of-file); often, you don't need to use feof() at all.

References: K&R2 Sec. 7.6 p. 164; ISO Sec. 7.9.3, Sec. 7.9.7.1,
Sec. 7.9.10.2; H&S Sec. 15.14 p. 382.

--
"This is a wonderful answer.
It's off-topic, it's incorrect, and it doesn't answer the question."
--Richard Heathfield
Mar 23 '07 #5

P: n/a
if you are having problems with EOF you are probably not opening the
file in binary mode ("rb")

use FILE *fp=fopen("file","rb");

also use when you use fread() you get the no .of bytes read till
EOF as the return value irrespective of the n passed ..
if no EOF is encountered Nis returned

use this n to write while using fwrite().

Mar 23 '07 #6

P: n/a
Ulrich Eckhardt wrote, On 23/03/07 16:13:
ericunfuk wrote:
>I want to read a binary file(it's pic.tif file, I guess it's binary
file?), then write it to a new file)

In general, all files are binary.
Not as far as C is concerned.
There are some OSs that have special
textfiles, where the system transparently translates lineendings.
There can be other differences as well, and this is why C makes a clear
distinction. You should therefore always open the file in the correct
mode unless you have a very good reason not to.
As far
as C is concerned, there is also the distinction between using e.g.
fprintf() which does formatting (as text) before writing and fwrite, which
takes preformatted data.
C does not make that distinction. C allows you to use fprintf on a
binary stream and fwrite on a text stream.

<snip>
>I'm very new to C so your help is much appreciated.

Get a good book, check out the reviews at ACCU's website.
Search the archives of this group and you will find references.
--
Flash Gordon
Mar 23 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.