CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2007
    Posts
    6

    Unhandled exception: 0xC0000005: Access violation

    hi all,
    i am new to MFC programming, i am Creating an object of CString to store an filename opened through dialog box.... If i pass this CString object as filename to fopen() function, I am getting Unhandled exception: 0xC0000005: Access violation..... so please help me out in this.....

    i.e.
    CString Filename;

    if( FileDlg.DoModal() == IDOK )
    {
    Filename = FileDlg.GetFileName();
    }


    And if i pass this Filename object to fopen() as
    fopen(Filename, "r");

    When I debug I get the violation exception at this line.....

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Unhandled exception: 0xC0000005: Access violation

    Are you using UNICODE and does fopen support UNICODE filenames ??

    btw : You are using MFC, so why use fopen ?? CFile is a better alternative.

  3. #3
    Join Date
    Apr 2007
    Posts
    6

    Re: Unhandled exception: 0xC0000005: Access violation

    i am using this same code of lines in another project... it's working fine......

    i am using fopen instead of using CFile class bcoz i am reading it character by character & also byte by byte after some condition..... So can u help me out of this......

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Unhandled exception: 0xC0000005: Access violation

    i am using fopen instead of using CFile class bcoz i am reading it character by character & also byte by byte after some condition..... So can u help me out of this......
    Byte by byte is very slow, because this creates a lot of disk access. Personally I would advice you to read the entire file (if it is not TOO big), and process your data in memory.

    btw : your crash is a mystery to me. Is there some code in between that uses/modifies the Filename string ? And is the file not locked by something else ?

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Unhandled exception: 0xC0000005: Access violation

    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Mar 2003
    Location
    The Netherlands
    Posts
    586

    Re: Unhandled exception: 0xC0000005: Access violation

    Code:
    char szBuf[BUFLEN];
    CString sFilename;
    
    if( FileDlg.DoModal() == IDOK )
    {
     sFilename = FileDlg.GetFileName();
     
     CFile file;
     if(file.Open(sFileName, CFile::modeRead))
     {
       DWORD dwSize = file.GetLength();
       if(dwSize < BUFLEN)
        file.Read(szBuf, (UINT)dwSize);
       file.Close();
    }
    Time is fun when you're having flies

  7. #7
    Join Date
    Apr 2007
    Posts
    6

    Re: Unhandled exception: 0xC0000005: Access violation

    thank you guys.....
    i finally got it.......
    Actually.. I created same code in another project, and it worked..... I really don't know what is the problem with my Visual Studio 6, so sorry guys for wasting your precious time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured