Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ios::app (fails to open file)

Status
Not open for further replies.

Sherak

Programmer
Sep 1, 2003
83
GB
I check if the file exists

if(FileExists(FileName))

And if it does not exist I open it like this:

OutputF.open(FileName, ios::eek:ut);

And all is fine, however if it does exist I open it like this:

OutputF.open(FileName, ios::app);

However in this case it wont open and returns OutputF.fail() as true?..... I can open the file to write over with the ios::eek:ut but if I try to open for append it wont open it, I have also tried ios::ate and the problem occurs, it has no problem with ios::eek:ut and ios::in though......

Any help is muchly appriciated :)
 
Try:
OutputF.open(FileName,ios_base::app);

This assumes FileName is an array of chars and OutputF is an ofstream, of course.
Hope this is some help!
DJL

A salesman is a machine for turning coke into obnoxious arrogance.

Common sense is what tells you the world is flat.

 
Thanks for that but alas it doesnt work... im stumped here is a section of my code:

if(FileExists(CustName))
{

OutputF.open(CustName, ios::app);
if(OutputF.fail()){Application->MessageBoxA("Could not open output file please contact your system administrator","Error",MB_OK);
return false;
}

}
else
{
OutputF.open(CustName, ios::eek:ut);
if(OutputF.fail()){Application->MessageBoxA("Could not open output file please contact your system administrator","Error",MB_OK);return false;}
}

now when the file deos not exist it takes the 'else' and creates the file no problem, then when I run it again the file exists so it takes the 'if' as it should however it reports the open as a fail?...
 
It looks like it should work. Here is a section of code from a recent app I wrote that works:
Code:
// Open streams
std::ofstream ParcelLogStrm;
ParcelLogStrm.open(ParcelLogFile.c_str(), std::ios::app);
if (!ParcelLogStrm)
{
    Application->MessageBox("Cannot open Parcel/Log file for output!", "File Error", MB_OK);
    return;
}
else
{
.
.
.
}

My ParcelLogFile is an AnsiString hence the .c_str(). Since I don't explictly call the standard namespace, I use the "std::" in front of standard streams.

One note, ios::app and ios::ate will create the file for you if it doesn't already exist so you really don't have to check to see if the files exists although it doesn't hurt either.

James P. Cottingham

There's no place like 127.0.0.1.
There's no place like 127.0.0.1.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top