473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

read and write binary file

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
6 7339
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
"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
>
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 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
Mar 23 '07 #4
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: spike | last post by:
Im trying to write a program that should read through a binary file searching for the character sequence "\name\" Then it should read the characters following the "\name\" sequence until a NULL...
8
by: Patrik Malmström | last post by:
How do I read, write a file binary? I want to open, say, file.exe read it in to the program, then write it out to file2.exe. Like file copy, anyone have a code sample?
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
1
by: vinothg | last post by:
I have a binary file,which contains strings of 30 bytes each.I need to open the file,read the strings one by one and if the string is not found i need to write it.But unfortunately both read and...
3
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
I try to follow Steve's paper to build a database, and store a small text file into SQL Server database and retrieve it later. Only difference between my table and Steve's table is that I use NTEXT...
9
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data =...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
1
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.