473,414 Members | 1,738 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,414 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 2567
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 (40°39.22'N, 111°50.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....
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.