59518

Exception while opening file

Question:

I have a VC++ application and in my application i have some basic file operations.

Below is the defaulting code

CStdioFile cFile; CFileException e; CString sReport; CString sHtmlfile = "testreport.html" OutputDebugString((sHtmlfile)); if (!cFile.Open(sHtmlfile,CFile::modeCreate | CFile::modeWrite, &e )) { }

The problem is my application executes this piece of code every few minutes. and it works fine.

After several runs of the code the cFile.Open() function fails. I tried to get the error message

TCHAR szError[1024]; e.GetErrorMessage(szError,1024); OutputDebugString((szError));

The irony is the szError error message is "No error occured".

This again works once i restart my application. Any idea why this occurs.

Thanks in advance.

Answer1:

Do you have multiple instances running? I suggest you use <a href="http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx" rel="nofollow">Process Explorer</a> when the error occurs to see if any other handles to the said file exist.

And GetLastError will report the error reported by the last API function. If there were any other API calls between the failing API call and the call to GetLastError, then the last error value is overwritten. (As @sbi has already pointed out in the comments.)

Answer2:

Maube you forget to close your file and it come out of file descriptors. They are all closed when you quit your application, then you can run it again. Check if your files are closed or not.

OK. If this is not the above case, what could it be ? You get the error message from cFile.Open, hence we can believe it's accurate.

I'm not sure what would happen if another file of the same name is already open by the current process, or if you try to open a file with a strange name, like empty string. To sort these out you can also print the name of the file you are opening with the error (and also trace the cases where no error occurs).

Answer3:

You are using C++. Your error could be completely somewhere else. I've had a pointer bug that resulted in clean code coughing up an error.

Have you tried building in release mode?

I would suggest trying to step through and perhaps narrow down where your error appears.

Recommend

  • foreach async function in node.js
  • how to use Get method for calling web services in android
  • How to login to the Administrator page in PHP
  • SQLITE: database is locked error (error - 5) when used update query
  • Approximate string matching algorithms state-of-the-art [closed]
  • Trying to find and install libstdc++.so.5 to run C++ with mex for MATLAB
  • How do I modify this page peel effect without messing it up?
  • Flash Movies and ActionScript - Stop the video from working offline
  • VBA Dir function not working on Excel 2010
  • Vuejs: Lifecycle hooks of child routerview components using keep alive
  • Android onKey w/ virtual keyboard
  • extjs4 catching the scroll event on panel
  • chrome video src change not working
  • How can I run DataNucleus Bytecode Enhancer from SBT?
  • Repository Browser Only - \"Repository moved permanently to… please relocate”
  • CSS bleed-through with cfinput type=“datefield”
  • Group list of tuples by item
  • npm 5.4.1 install/uninstall all failing
  • Cannot upload to OneDrive using the new SDK
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • print() is showing quotation marks in results
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • Lost migrations and Azure database is now out of sync
  • Get one-time binding to work for ng-if
  • Illegal mix of collations for operation for date/time comparison
  • Dynamically accessing properties of knockoutjs observable array
  • Jenkins: How To Build multiple projects from a TFS repository?
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • ILMerge & Keep Assembly Name
  • Running a C# exe file
  • Large data - storage and query
  • How can I estimate amount of memory left with calling System.gc()?
  • WOWZA + RTMP + HTML5 Playback?
  • A cron job substitute?
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass