472,780 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 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 7243
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.