Jump to content
  • Advertisement
Sign in to follow this  
afraidofdark

[c++] fstrem.open() causes ERROR_ALREADY_EXISTS

This topic is 2696 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[font="verdana, arial, helvetica, sans-serif"]hello, I wrote a simple logger, which should print last error through win32 Api function GetLastError. However, logger itself creates the last error and false feed back caused me a day.

_file.open("log.txt", std::fstream::out | std::fstream::trunc); this line generate ERROR_ALREADY_EXISTS log.txt is already exist, yes it does exist, I just want to override the actual one.


_file.open("log.txt", ios::app); this line is another problem, I just want to open the existing file and append to new text to it but this line generates the same error.

How can I do these operations without getting error ?

thank you for your replies.




#ifndef AMZ_LOGGER_H
#define AMZ_LOGGER_H

#include "amzPrerequisite.h"

namespace amz {

class _AmzExport Logger {
public:

static Logger* getLogger() {
if(_logger == 0) {
_logger = new Logger;
}
return _logger;
}

void log(String msg) {
msg.append("\n");

_file.open("log.txt", ios::app);
assert(_file.is_open());

_file.write(msg.c_str(),msg.length());
_file.close();
}

~Logger() {
_file.close();
}

protected:

Logger() {
// create a log file
_file.open("log.txt", std::fstream::out | std::fstream::trunc);
assert(_file.is_open());
_file.close();
}

private:
static Logger* _logger;
FileStream _file;

};

}

#endif

[/font]

Share this post


Link to post
Share on other sites
Advertisement
So does the file successfully get updated with the open/write/close functions? I wonder if its picking up that "the file does indeed exist" but handling it in the fstream functions properly because of the modes given.

I have heard some cool things about Win32 API, maybe this is one of its features.

Share this post


Link to post
Share on other sites

So does the file successfully get updated with the open/write/close functions? I wonder if its picking up that "the file does indeed exist" but handling it in the fstream functions properly because of the modes given.

I have heard some cool things about Win32 API, maybe this is one of its features.



yes it handles file properly, and I really don't want to see this error because there isn't anything wrong. When an actual error happens, I try to log it, however last error is always ERROR_ALREADY_EXISTS and this (IMHO) buggy behavior caused me spending a day to debugging and bug still hasn't solved

by the way, I hope you understand my English, sorry for my bad English

Share this post


Link to post
Share on other sites
I think the first example could be overcome by deleting the file if it already exists. I think you are basically starting over with its data while reusing its name, right?

For the 2nd example you might have to get more creative like copy "log.txt" to "_log.txt" and then delete "log.txt". Then open new file "log.txt" copy over the data from "_log.txt", then delete "_log.txt" and continue on working with "log.txt". Sounds pretty lame, but so is getting that error when its not really an error for you.

Share this post


Link to post
Share on other sites
I don't think this is buggy behaviour; I actually think that the root cause is that you're mixing fstream with Windows API calls.

I'm betting that on Windows, fstream::open winds up calling CreateFile behind the scenes (standard CRT fopen does and I see no reason for fstream::open to be any different). Per the documentation for CreateFile (clicky), when the CREATE_ALWAYS flag is set, "If the specified file exists and is writable, the function overwrites the file, the function succeeds, and last-error code is set to ERROR_ALREADY_EXISTS (183)."

In other words it's actually documented behaviour and may even be desirable as sometimes you might want to know if the file was overwritten. True, the fact that it sets an error flag is slightly misleading, but nonetheless.

Why not just use fstream's own error checking instead of Win32?

Share this post


Link to post
Share on other sites

Why not just use fstream's own error checking instead of Win32?
[/quote]
I believe the OP wants to use the fstream to log Win32 errors, and this behaviour is causing the error to be lost before it can be logged. Hence my suggestion. I could be misinterpreting though.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!