Writing to an existing file.

S

shalakasan

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
 
D

Default User

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
 
K

Keith Thompson

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

shalakasan

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?
 
C

Chris Torek

Thanks for ur input.

Ur-input? That must be some sort of ancient prototype input
(see said:
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.)
 
B

Bill Pursell

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

Keith Thompson

Bill Pursell said:
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.
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top