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

Writing to an existing file.

P: n/a
Hi,

I want to open a binary file and write to a specific location in File.
Here is how my code looks like:

int main()
{

long lbuf = 0;
int lreadBytes = 0;
long lValue = 0;

FILE *pFile = fopen ("c:\\TestBin.bin","ab+");

fseek(pFile,4,SEEK_SET);

lValue = 222.4;

fwrite((char *)&lValue,1,sizeof(long),pFile);

fclose(pFile);
return 0;
}
This code writes to the end of file because I have opened it in append
mode. If I open in "wb+" mode the file gets erased. How do i write to
existing file?

Thanks and Regards,
Shal

Aug 1 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
sh********@gmail.com wrote:
Hi,

I want to open a binary file and write to a specific location in File.
Here is how my code looks like:

int main()
{

long lbuf = 0;
int lreadBytes = 0;
long lValue = 0;

FILE *pFile = fopen ("c:\\TestBin.bin","ab+");

fseek(pFile,4,SEEK_SET);

lValue = 222.4;

fwrite((char *)&lValue,1,sizeof(long),pFile);

fclose(pFile);
return 0;
}
This code writes to the end of file because I have opened it in append
mode. If I open in "wb+" mode the file gets erased. How do i write to
existing file?
"rb+". Your book should explain this.

Brian

Aug 1 '06 #2

P: n/a
sh********@gmail.com writes:
I want to open a binary file and write to a specific location in File.
Here is how my code looks like:

int main()
{

long lbuf = 0;
int lreadBytes = 0;
long lValue = 0;

FILE *pFile = fopen ("c:\\TestBin.bin","ab+");

fseek(pFile,4,SEEK_SET);

lValue = 222.4;

fwrite((char *)&lValue,1,sizeof(long),pFile);

fclose(pFile);
return 0;
}
This code writes to the end of file because I have opened it in append
mode. If I open in "wb+" mode the file gets erased. How do i write to
existing file?
As Brian said, you're looking for "rb+".

A few other points:

You need "#include <stdio.h>".

"int main()" is acceptable, but "int main(void)" is better.

You declare lbuf and lreadBytes, but you never use them.

Always check the result of fopen().

The first argument to fwrite() is of type void*. Since any object
pointer type can be implicitly converted to void*, the cast to char*
is unnecessary. (It happens to be harmless, but it's clutter.)
I would write the fwrite call as:

fwrite(&lValue, 1, sizeof lValue, pFile);

And of course I'd check the result.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 1 '06 #3

P: n/a
Thanks for ur input. But there is a problem with using "rb+" mode.
If the file does not exist then I want it to be created and "rb+" will
not create a file.
How do i achieve this?
Default User wrote:
sh********@gmail.com wrote:
Hi,

I want to open a binary file and write to a specific location in File.
Here is how my code looks like:

int main()
{

long lbuf = 0;
int lreadBytes = 0;
long lValue = 0;

FILE *pFile = fopen ("c:\\TestBin.bin","ab+");

fseek(pFile,4,SEEK_SET);

lValue = 222.4;

fwrite((char *)&lValue,1,sizeof(long),pFile);

fclose(pFile);
return 0;
}
This code writes to the end of file because I have opened it in append
mode. If I open in "wb+" mode the file gets erased. How do i write to
existing file?

"rb+". Your book should explain this.

Brian
Aug 3 '06 #4

P: n/a
In article <11**********************@m79g2000cwm.googlegroups .com>
<sh********@gmail.comwrote:
>Thanks for ur input.
Ur-input? That must be some sort of ancient prototype input
(see <http://dictionary.reference.com/search?q=ur>).
>But there is a problem with using "rb+" mode.
If the file does not exist then I want it to be created and "rb+" will
not create a file.
How do i achieve this?
There is the "portable method", and then there is the "good method".

The portable method is to use "w+". As you have seen, this wipes
out the existing file. So use it only if a first attempt with "r+"
fails.

This method is not "good" because there are all kinds of reasons
for "r+" to fail other than "file simply did not exist". Alas,
this is all you get in portable C. The "good" method -- along with
"how much better it is than the portable method" -- generally varies
from one system to another. This means that if you write code to
use this "better way", it will only work on a few systems, instead
of every hosted system.

It is up to you to decide whether the "good" method, whatever that
may be, is more important than the portability you lose by using
it.

(It would be nice if Standard C had a way to say "open file for
reading and writing, creating file if needed but not destroying
existing data if file already exists". But it does not, and
trying "r+" then "w+" is all we get in Standard C.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Aug 3 '06 #5

P: n/a

Keith Thompson wrote:
sh********@gmail.com writes:
I want to open a binary file and write to a specific location in File.
Here is how my code looks like:
<snip>

fwrite((char *)&lValue,1,sizeof(long),pFile);
fwrite(&lValue, 1, sizeof lValue, pFile);
Ouch! I'd write it as:

fwrite(&lValue, sizeof lValue, 1, pFile);

Much less obscure, and easier to avoid a stupid mistake
when checking the return value.

Aug 4 '06 #6

P: n/a
"Bill Pursell" <bi**********@gmail.comwrites:
Keith Thompson wrote:
>sh********@gmail.com writes:
I want to open a binary file and write to a specific location in File.
Here is how my code looks like:
<snip>
>
fwrite((char *)&lValue,1,sizeof(long),pFile);
> fwrite(&lValue, 1, sizeof lValue, pFile);

Ouch! I'd write it as:

fwrite(&lValue, sizeof lValue, 1, pFile);

Much less obscure, and easier to avoid a stupid mistake
when checking the return value.
Yes, you're right. The second and third parameters are the size in
bytes of a single element and the number of elements, respectively,
and the returned value is the number of elements successfully written.
I didn't bother to check the order.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 4 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.