I have an old project in Visual C++ and I'm trying to migrate it to Visual Studio 2013. When I verify if a txt file exists, CFile returns debug assertion error. The code is:
if (!txt_file.Open(txt_name, CFile::modeWrite | CFile::shareDenyWrite | CFile::typeText))
{
//action if the file exists
}
What is the problem, I'm doing something wrong ? Thank you
L.E. :
txt_file is declared as : CStdioFile txt_file in the class trace
txt_name is declared as : private CString txt_name in the method named open_file from class trace
The method open_file contains the if statement that returns debug assertion error.
You are probably using:
CFiledoes not support text mode.To open in text mode, you can change to:
That should fix the problem (at least, using
CFilein this case generates an assertion).If you are using
CStdioFilealready, there's probably a problem with the (combination of) open modes. As a test, try to removeCFile::shareDenyWrite. There could be security restrictions too.It might be best to step through it with the debugger, or have a look at
filecore.cpp Line: 179to see what gets checked there (I would look it up for you, but don't have Visual Studio 2013 at hand right now - probably open modes).Update:
The file is already opened. So no need to open it again, or first needs to be closed, to open with other open modes.
Or to test if the file is open (not valid for
CMemFile):