CFile in MFC

This is a discussion on CFile in MFC within the Windows Programming forums, part of the Platform Specific Boards category; i am having problems writing to a file. i am getting an access violation writing to the file. ive dug ...

  1. #1
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465

    CFile in MFC

    i am having problems writing to a file. i am getting an access violation writing to the file. ive dug through the documentation for about an hour and i still cant figure out what im doing wrong.

    here is my save code

    Code:
    void CChildView::SaveMap( CString path )
    {
    	CFile file;
    
    	const int SizeOfBuffer = mapSz.x * mapSz.y;
    	int * buffer = new int [ SizeOfBuffer ];
    
    	int sizeBuffer[2];
    
    	try 
    	{
    		file.Open( path, CFile::modeCreate );
    
    		sizeBuffer[0] = mapSz.x;
    		sizeBuffer[1] = mapSz.y;
    
    		for ( int i = 0; i < mapSz.y; ++i )
    		{
    			for ( int j = 0; j < mapSz.x; ++j )
    			{
    				buffer[i * mapSz.x + j] = map[i][j];
    			}
    		}
    
    		file.Write( sizeBuffer, 2 * sizeof(int));
    		file.Write( buffer, SizeOfBuffer * sizeof(int));
    
    	}
    
    	catch (void)
    	{
    		MessageBox( "An error occured while writing to the file" );
    	}
    
    	delete [] buffer;
    
    	file.Close();
    
    	mapHasBeenEdited = FALSE;
    	
    }
    Last edited by ...; 10-21-2003 at 09:22 PM.
    I came up with a cool phrase to put down here, but i forgot it...

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    CFile Open() does not throw an exception.

    You have to test the return != FALSE

    Does the path contain the correct file name (try setting the string locally to create a test file)

    Does the file create ie return TRUE?

    If not what does GetLastError() return?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    Read takes a pointer to a buffer and the number of bytes to read and returns the actual number of bytes that were read. If the required number of bytes could not be read because end-of-file (EOF) is reached, the actual number of bytes read is returned. If any read error occurs, an exception is thrown. Write is similar to Read, but the number of bytes written is not returned. If a write error occurs, including not writing all the bytes specified, an exception is thrown.
    straight from MSDN documentation. i put the whole file opening/writing process in the try block just to be on the safe side.

    everything returns true, everything works fine, a file is created, but the file is always empty and i get an "access to (pathname) was denied" error.

    i ran it through debugger and it choked on this line :

    Code:
    file.Write( sizeBuffer, 2 * sizeof(int));
    file.Write( buffer, SizeOfBuffer * sizeof(int));
    I came up with a cool phrase to put down here, but i forgot it...

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    OK so the file is created.

    We can move on and look at the writing functions

    try

    CFile::modeCreate | CFile::modeWrite

    if this does not help

    then try using a char buffer to write data to the file.

    use the 'path' buffer as you know what it contains.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    ive already tried using all of the different modes, and i just tried writing a string to the file with no luck. the file is still empty.

    same error, same place.
    I came up with a cool phrase to put down here, but i forgot it...

  6. #6
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Do you have permissions on the file for these operations? Your being denied access to the newly created file. Perhaps it is a lack of or improperly set permissions on your computer if you're using Linux, Unix, Windows NT, Windows 2000, or Windows XP.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WIndows programming?
    By hostensteffa in forum Windows Programming
    Replies: 7
    Last Post: 06-07-2002, 08:52 PM
  2. Release MFC Programs & Dynamic MFC DLL :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-18-2002, 06:42 PM
  3. Understanding The Future of MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 04-15-2002, 09:08 PM
  4. Beginning MFC (Prosise) Part III - Now What? :: C++
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 03-03-2002, 05:58 PM
  5. MFC is Challenging :: C++
    By kuphryn in forum C++ Programming
    Replies: 8
    Last Post: 02-05-2002, 12:33 AM

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