473,399 Members | 3,603 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,399 software developers and data experts.

nocreate problem

Hello all,
I have the following problem.
I have a function which being called several times during the run of
my program. When the function is called for the first time I want it
to create a file and write something to that file. Regardless if the
file
exist or not. If it exist it should truncate it and write it as if it
has
not existed. But when the next time I call the function I want it to
test
if the file still exist or not. If not it should not create it. If it
does exist then I want it to append the file. But std does not support
nocreate anylonger. But how can I solve this problem now ??
Thank you in advance.
Robert

void writefile()
{
std::ofstream fout;
if( forthefirstime )
{
fout.open( FileName.c_str(), std::ios::trunc );
if( !fout )
{
std::cerr<<"can't open file";
}
fout<<"this is my first sentence"<<std::endl;
}
else
{
fout.open( FileName.c_str(), std::ios::nocreate | std::ios::app );

if( !fout )
{
std::cerr<<"can't open file";
}
fout<<"this is the next sentence"<<std::endl;

}
fout.close();
forthefirsttime = false;
}

Jul 23 '05 #1
1 3294
wo*********@yahoo.com wrote:
Hello all,
I have the following problem.
I have a function which being called several times during the run of
my program. When the function is called for the first time I want it
to create a file and write something to that file. Regardless if the
file
exist or not. If it exist it should truncate it and write it as if it
has
not existed. But when the next time I call the function I want it to
test
if the file still exist or not. If not it should not create it. If it
does exist then I want it to append the file. But std does not support
nocreate anylonger. But how can I solve this problem now ??
Thank you in advance.
Robert

void writefile()
{
std::ofstream fout;
if( forthefirstime )
{
fout.open( FileName.c_str(), std::ios::trunc );
if( !fout )
{
std::cerr<<"can't open file";
}
fout<<"this is my first sentence"<<std::endl;
}
else
{
fout.open( FileName.c_str(), std::ios::nocreate | std::ios::app );

if( !fout )
{
std::cerr<<"can't open file";
}
fout<<"this is the next sentence"<<std::endl;

}
fout.close();
forthefirsttime = false;
}


instead of opening with ios::nocreate open it with ios::in. If this
fails the file does not exist. if it exists you can reopen it for
writing...

Tom
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
117
by: Peter Olcott | last post by:
www.halting-problem.com
3
by: HLSL fan | last post by:
In the new VC++ 7.0 ios::nocreate is obsolete. Does any one know what I should do to achieve the same goal? I really want to check if a file exists before reading from it. Thanks in advance. ...
1
by: Gernot Frisch | last post by:
std::ifstream ifs(cFileMitExtdat, std::ios::nocreate); gives: error C2039: 'nocreate': Is not an element of 'std::basic_ios<_Elem,_Traits>' with
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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...

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.