Tech Support > Microsoft Windows > Development Resources > error 183: "Cannot create a file..." when using std::ofstream
error 183: "Cannot create a file..." when using std::ofstream
Posted by Andreas Schmitt on March 26th, 2007


I am opening a file in my application with
std:fstream theStream;
theStream.open("file.txt", std::ios_base:ut | std::ios_base::app);
I am using that file among other things to log system error messages. I
noticed that the open() method
itself causes such an error if "file.txt" does already exist:

error 183 - ERROR_ALREADY_EXISTS
Cannot create a file when that file already exists.


How do I get rid of that error message? Writing to the file is in no way
affected by this, the stream
works perfectly. Why does this cause a windows error message if the file is
already there?

Thanks in advance


Posted by Ulrich Eckhardt on March 26th, 2007


Andreas Schmitt wrote:
Note: the 'ios_base:ut' is implicitly given by the fact that it's
an 'ofstream', it is only needed with plain 'fstream'.

Where do you see that error? Normally, this should neither write to one of
the standard output streams nor pop up a window....

Uli

--
Sator Laser GmbH
Geschäftsführer: Ronald Boers Steuernummer: 02/858/00757
Amtsgericht Hamburg HR B62 932 USt-Id.Nr.: DE183047360


Posted by Andreas Schmitt on March 26th, 2007


I am using the filestream to log certain things. Among other functions the
object
wrapping the filestream can also write the latest system error to the
stream.
The error is made available by the Windows SDK function GetLastError().

This doesn't pop up or is written anywhere when I access the last error,
it's there.
It wouldn't disturb the program itself or anything, and the error would
probably not
show up in the log since I only log the last error if I some system function
has informed me that something has gone wrong first, but still... it's an
error.

So there should be a way to open the file without causing that error?



Posted by Heinz Ozwirk on March 26th, 2007


"Andreas Schmitt" <KeldorKatarn@gmx.de> schrieb im Newsbeitrag
news:eu943i$iq8$01$1@news.t-online.com...
....
GetLastError does just that. It returns the last error, that has been
detecte before its call. This may be hours ago or even weeks. If the
function you have called does not return an error, you have no reason to
call GetLastError. Even if the result of GetLastError changed during some
operation, it is meaningless as long as the operation as a whole succeeds.

When you try to open a file and the file is open afterwards, then there is
no error to worry about. Never use GetLastError to detect the presence of an
error. Only use it to find the cause of an error once another function told
you that there actually is an error.

HTH
Heinz



Posted by Andreas Schmitt on March 26th, 2007


I will probably ignore it, since I don't see how to fix it.. but the error
is 100% caused by the
filestream.open() operation, since when I start the program twice, the first
time when the file
isn't existing yet, I have a code 0 - operation was successfull message in
the log, after that
error code 183 - file already exists.

As I said, this won't affect the oparations of my program in any way, I was
just looking at this
from a perfectionist's way of saying "If there's an error, then there must
be a way to fix it"

It is possible of course that windows reacts like this because of some way
drinkumware wrote
the STL basic_fstream class.. in which case I wouldn't be able to fix this
anyway.

I was just wondering if there was any way I could prevent this from showing
up at all, again, from a
perfectionist's point of view.

If you all say.. no.. nevermind.. go ahead.. then alright..



Posted by Andreas Schmitt on March 26th, 2007


Just to explain further how I ran into this error in the first place... I
usually DON'T call GetLastError()
regulary or to detect any errors. I use it the way it's supposed to be used.
To log an error if some system
function tells me something went wrong.
I ran into this specific error by simply calling my log-function to see if
it works as it is supposed to, meaning
if it does log anything at all.
I was just a little surprised to actually find an error message in the log
afterwards although nothing had really
gone wrong. That's all.


Posted by Grzegorz Wróbel on March 26th, 2007


Heinz Ozwirk wrote:
"Never say never".

There are some rare cases when you do need to call GetLastError() to
detect whether function actually succeeded to perform required task or
not. Take as an example AdjustTokenPrivileges API.

--
Grzegorz Wróbel
http://www.4neurons.com/
677265676F727940346E6575726F6E732E636F6D

Posted by Ulrich Eckhardt on March 27th, 2007


Andreas, please don't snip attribution lines. If you quote me, you owe me
the respect of at least acknowledging that. Also, btw, your newsclient
generates broken formatting, I guess it's OE - search the web for fixes,
they exist, or get a real one.

Now...

Andreas Schmitt wrote:
This is meaningless in the context of C++ iostreams. They might or might not
set the last error of the win32 API, but they don't use it to signal
errors. Ignore that value unless you call a function where it is documented
that it sets this in case of an error. Also, in that case, call
GetLastError() before calling any other function that might modify it.
Typically, you best store the error-state immediately afterwards.

The 'error' (I wouldn't call it that, it is just one more possible outcome
of an operation) is already handled. You don't have to do anything. What
probably happens is that the ofstream first tries to create the file, and
then it is told that it already exists. It then simply continues with
opening the existing file and setting the write position to the end. No
error.

Uli

--
Sator Laser GmbH
Geschäftsführer: Ronald Boers Steuernummer: 02/858/00757
Amtsgericht Hamburg HR B62 932 USt-Id.Nr.: DE183047360