read and write binary file

E

ericunfuk

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.
 
U

Ulrich Eckhardt

ericunfuk said:
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
 
B

Ben Pfaff

ericunfuk said:
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.
 
E

ericunfuk

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!


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 Igive 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
 
B

Ben Pfaff

ericunfuk said:
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.
 
D

daya

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().
 
F

Flash Gordon

Ulrich Eckhardt wrote, On 23/03/07 16:13:
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.

Get a good book, check out the reviews at ACCU's website.

Search the archives of this group and you will find references.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top