none
Access violation writing location exception when writing to file C++

    Question

  • Ok so I'm trying to write just basic text to a file.  And everytime I try to write anything to the file I get this exception:

    Unhandled exception at 0x00417cb4 in C++FileIO1.exe: 0xC0000005: Access violation writing location 0x0041d73e.

    Here is my full code:

    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    int _tmain(int argc, _TCHAR* argv[])
    {
    	bool exit1=false;
    	while (exit1==false)
    	{
    		char* fpath="";
    		char* fdata="";
    		cout << "Please input the path of the file you want to write to: ";
    		cin >> fpath;
    		if (fpath == "/exit") 
    		{
    			exit1=true;
    		}
    		fstream files1;
    		files1.open(fpath);
    		if (!files1) {
    		cout << "\n" << "\n" << "Please input the data you would like to write to the file:" << "\n";
    		cin >> fdata;
    		files1 << "test";
    		}
    	}
    	cin.get();
    	return 0;
    }
    
    Any help would be appreciated.

    Tuesday, February 02, 2010 4:46 AM

Answers

  • Mitch1423 wrote:
    > Ok so I'm trying to write just basic text to a file.  And everytime I try to write anything to the file I get this exception:
    >
    > Unhandled exception at 0x00417cb4 in C++FileIO1.exe: 0xC0000005: Access violation writing location 0x0041d73e.
    >
    >   char* fdata="";
    >   cin >> fdata;
    fdata points to a string literal. String literals are stored in read-only memory. You need a writable buffer where user input can be stored.
     
    Consider using std::string instead. Your life will be so much easier.
    --
    Igor Tandetnik
    • Marked as answer by Nancy Shao Monday, February 08, 2010 3:26 AM
    Tuesday, February 02, 2010 5:22 AM
  • As Igor suggest you can use a string class like std:string or CString (MFC), or, if you're trying not to use any classes at all you could allocate real memory for the fdata variable like:

    char fdata[1000]; // 1000 chars for buffer

    Then you'd have real memory for cin to use.

    Tom

    In the particular context posted by the OP, I would suggest using std::string like Igor did (but in other contexts, like Win32 programming, my preference is for CString ATL/MFC class).

    The problem of CString in that particular context, is that it does not play well with cin. I mean, code like this:

      CString str;
      cin >> str;

    fails to compile because of lack of proper operator>> overload defined for CString instances.

    Instead:

      string s;
      cin >> s;

    would work just fine.

    On the other hand, I'm against raw C-like buffer like char data[1000]; in fact, there could be potential buffer overruns and consequent security problems in the app.

    Giovanni



    • Marked as answer by Nancy Shao Monday, February 08, 2010 3:26 AM
    Wednesday, February 03, 2010 11:56 AM

All replies

  • Mitch1423 wrote:
    > Ok so I'm trying to write just basic text to a file.  And everytime I try to write anything to the file I get this exception:
    >
    > Unhandled exception at 0x00417cb4 in C++FileIO1.exe: 0xC0000005: Access violation writing location 0x0041d73e.
    >
    >   char* fdata="";
    >   cin >> fdata;
    fdata points to a string literal. String literals are stored in read-only memory. You need a writable buffer where user input can be stored.
     
    Consider using std::string instead. Your life will be so much easier.
    --
    Igor Tandetnik
    • Marked as answer by Nancy Shao Monday, February 08, 2010 3:26 AM
    Tuesday, February 02, 2010 5:22 AM
  • As Igor suggest you can use a string class like std:string or CString (MFC), or, if you're trying not to use any classes at all you could allocate real memory for the fdata variable like:

    char fdata[1000]; // 1000 chars for buffer

    Then you'd have real memory for cin to use.

    Tom
    Wednesday, February 03, 2010 6:54 AM
  • As Igor suggest you can use a string class like std:string or CString (MFC), or, if you're trying not to use any classes at all you could allocate real memory for the fdata variable like:

    char fdata[1000]; // 1000 chars for buffer

    Then you'd have real memory for cin to use.

    Tom

    In the particular context posted by the OP, I would suggest using std::string like Igor did (but in other contexts, like Win32 programming, my preference is for CString ATL/MFC class).

    The problem of CString in that particular context, is that it does not play well with cin. I mean, code like this:

      CString str;
      cin >> str;

    fails to compile because of lack of proper operator>> overload defined for CString instances.

    Instead:

      string s;
      cin >> s;

    would work just fine.

    On the other hand, I'm against raw C-like buffer like char data[1000]; in fact, there could be potential buffer overruns and consequent security problems in the app.

    Giovanni



    • Marked as answer by Nancy Shao Monday, February 08, 2010 3:26 AM
    Wednesday, February 03, 2010 11:56 AM
  • Good point G.  Of course, OP could use GetBuffer() or case the CString with (LPCTSTR), but otherwise they are difficult to use in that context.  I don't like using the buffer idea either, but I listed it in case OP was trying to do it without STL or MFC.

    Tom
    Wednesday, February 03, 2010 9:37 PM