rkellerm
rkellerm

Reputation: 5522

ofstream operator<< access violation error

I have a small application that was written for Linux and now was ported to Windows. It's single-threaded and uses boost (If it matters).
The application is trying to write to a text file using the following code:

m_oFile.open(oFileName.c_str());
if(!m_oFile.is_open())
{
    cerr << "Unable to open output file: " <<  oFileName.c_str() << endl;
    exit(0);
}

m_oFile << "some text goes here\n";

m_oFile is a member of the class.
The file is created and opened successfully; the exception is thrown at the last code line above.

Stack trace:

msvcr100.dll!_lock_file(_iobuf * pf)  Line 236 + 0xa bytes  C   
App.exe!std::basic_filebuf<char,std::char_traits<char> >::_Lock() Line 310 + 0xf bytes  C++   
App.exe!std::basic_ostream<char,std::char_traits<char> >::_Sentry_base::_Sentry_base(std::basic_ostream<char,std::char_traits<char> > & _Ostr)  Line 93 + 0x30 bytes    C++
App.exe!std::basic_ostream<char,std::char_traits<char> >::sentry::sentry(std::basic_ostream<char,std::char_traits<char> > & _Ostr)  Line 114 +  x3a bytes   C++

Thanks!

EDIT:

When I changed the Code Generation properties to use Multi Threaded Debug Dll (/MDd) instead of Multi Threaded Dll (/MD) everything runs properly. Do you have any explanation for that?
Thanks again.

Upvotes: 2

Views: 1660

Answers (2)

user2249683
user2249683

Reputation:

Oh - the msvc incompatibility of libraries nightmare - yep make sure all(!) libraries match each other.

  • no mixing debug an release
  • not mixing single and multi-threaded
  • ...

Upvotes: 1

Luca Davanzo
Luca Davanzo

Reputation: 21528

Take a look here and try to open your file in read/write mode.

m_oFile.open(oFileName.c_str(), ios::out | ios::in);

Otherwise, check file's permissions.

edit

Remember to always close your file at the end.

m_oFile.close();

Another hit: could the file be used from another thread?

Upvotes: 0

Related Questions