Open file for output fails - really trivial code

This is a discussion on Open file for output fails - really trivial code within the C++ Programming forums, part of the General Programming Boards category; Here's my code, this is a trivial test program I wrote to isolate the problem. Code: #include <fstream> #include <string> ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    Open file for output fails - really trivial code

    Here's my code, this is a trivial test program I wrote to isolate the problem.

    Code:
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    void main()
    {
    	fstream    file;
    	char         filename[255];
    
    	strcpy(filename,"C:\\temp\\test.txt");
    	file.open(filename, fstream::out);
    
    }
    When I run it with debugging it throws an exception in open.c, in the function _tsopen_nolock(...), at the line

    Code:
    if ( (osfh = CreateFile( (LPTSTR)path,
                                     fileaccess,
                                     fileshare,
                                     &SecurityAttributes,
                                     filecreate,
                                     fileattrib,
                                     NULL ))
                 == (HANDLE)(-1) )
    The exception message is:
    Unhandled exception at 0x7721631f in TestProject.exe: 0xC0000008: An invalid handle was specified.
    Note that this is NOT just a "first-chance" exception from the debugger that I can ignore, even if I continue I get the same exception again, without the "first-chance" qualifier, and the file is not successfully opened and cannot be written to.

    After some digging around with WinDbg and searching online I found that the cause of this exception is:
    Invalid Handle exception (0xC0000008) can frequently be seen in crash dumps. It results from an invalid handle value passed to CloseHandle and other Win32 API or when a handle or return status is checked manually for validity and the same exception is raised via RaiseException or internally via RtlRaiseStatus.
    ... but that doesn't help me very much.

    Once the exception it thrown if I go to the specified location then the file HAS been created, and Windows tells me I can't delete it because "the file is open in TestProject.exe".

    Finally, if I run the program using Ctrl-F5, start program without debugging, then it runs perfectly and I can write to the file.

    This problem seems to just have come out of nowhere. I can't think of anything that I changed between last Friday when it was working and yesterday when it this started happening. Any ideas GREATLY appreciated.

    <EDIT> I forgot to mention I'm using MS Visual C++ 2010 Express </EDIT>
    Last edited by robertwinz; 08-02-2011 at 02:57 PM. Reason: add IDE information

  2. #2
    Registered User
    Join Date
    Aug 2011
    Posts
    2
    I have just come across precisely the same problem. One further piece of evidence: in my case the error only occurs when trying to open the file on the C: drive. When using another drive, everything works correctly.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Location
    Bristol, UK
    Posts
    2
    I also have just discovered this issue; for me it happens when I'm debugging a call to CreateFile that has GENERIC_WRITE set.... It happens irrespective of the location....
    I haven't changed this portion of code for ages and I know it was working perfectly a while back; pre VS2010 SP1 ..

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    1

    Found this problem as well

    Identical issue, invalid handle specified errors thrown inside the MSVC 2008 or 2010 IDE while the program works fine outside of it. Also D3dcreatedevice throws invalid handle specified errors at me which is probably related.
    Also this is pretty universal and applies to any of my projects not just the latest(It applies to ones i've not touched in months and still havent touched).
    Only thing i can add for symptoms is that the getlasterror() function returns 6 after this problem has occured.
    I'm also on windows 7 and my code is reading not writing the text file.


    Addendum: Moving to the D drive instead of the C drive didn't help anything worst luck.
    Last edited by darkevilme; 08-02-2011 at 04:34 PM.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    I just did a System Restore to the middle of last week and the problem appears to have gone away. Seeing that other people have suddenly seen the same thing I'm suspecting windows update is the source of the problem...

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    People on MSDN forums are reporting that Trusteer Rapport is the cause of this problem - I do have that application installed, so uninstalling that might be an easier solution than System Restore.

    http://social.msdn.microsoft.com/For...d-1df3fa329c77

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    2

    Fixed

    Yes, uninstalling Trusteer Rapport (not just stopping it) fixed the problem for me.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Location
    Bristol, UK
    Posts
    2
    Fixed,

    Thanks guys, uninstalling Trusteer Rapport fixed the problem

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    Glad you fixed your problem. However, the code itself needs fixing, too. I shall point out a few things:

    First of all, void main is not standard. Don't use it.

    >>char filename[255];
    >>strcpy(filename,"C:\\temp\\test.txt");
    Unsafe. Vulnerable to buffer overflows. There is no reason to do that here.
    When needing to assign a string literal, you can do

    std::string s = "my text";

    >>fstream file;
    >>file.open(filename, fstream::out);
    Or you can just do
    fstream file(filename, fstream::out);
    Or
    fstream file("my file", fstream::out);
    Or
    fstream file(filename.c_str(), fstream::out);
    If filename is a std::string. That way, you don't need to use char arrays.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can't open two files at once, 2nd fopen fails
    By jeffmarc in forum C Programming
    Replies: 3
    Last Post: 09-18-2009, 03:06 PM
  2. fstream lets you open a file for output twice (?!)
    By DonFiasco in forum C++ Programming
    Replies: 1
    Last Post: 06-09-2005, 07:41 PM
  3. open a file NOT for in/output
    By mdbnkc in forum C++ Programming
    Replies: 7
    Last Post: 05-03-2004, 12:46 PM
  4. Replies: 22
    Last Post: 07-11-2002, 01:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21