473,246 Members | 2,884 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,246 software developers and data experts.

Writing to an existing file.

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
6 2560
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
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
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
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

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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
3
by: Mahesha | last post by:
Hello, I'm new to C++ and I have requirement to open a existing text file in write mode and write 2 new lines of text in the beginning of the file. I'm working with fstream standard library. If...
4
by: Thierry Lam | last post by:
Let's say I already wrote a file and have the following: testing testing testing testing testing testing Is there an easy way to write something of variable length at the top of the file? ...
10
by: Aaron | last post by:
Hello, I have a small application that I need to save data from 7 text boxes in to a csv file. This will entail btnNext_Click function that will create a new csv file and enter the 7 data fields...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
2
by: simonc | last post by:
Is there an easy way of writing a number in 32 bit integer format into four bytes of a file? I am experimenting with FilePut but I can only make it work by defining a four byte array and doing some...
1
by: nasirmajor | last post by:
Dear all i have the following code for writing to the existing file. =========================== void AppendToFileEng() { SqlDataReader gr = null; string engpath2=""; gr =...
5
by: rasmitasah25 | last post by:
hi, I am very new to perl.I have written a perl script which is writing data into an excel file.The problem is that it is creating one new excel file while executing but my need id to write into...
3
by: thanawala27 | last post by:
Hi, I had problems writing in an existing Excel File. There is an excel file with a worksheet. I would like to open this existing Excel file and add another worksheet and write data into it....
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.