• Advertisement
Sign in to follow this  

unique_ptr and fstream constructor problem

This topic is 2148 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm making a class that helps make working with CSV files easier, and many of my functions make use of an fstream object. So at first I declared an fstream object. However I ran into a problem with opening non-existent files, and I couldn't find any other way to have the my class create create one on the fly, if such a situation arose, except to use the fstream constructor. Consequently I decided to replace my fstream declaration with a smart pointer to an fstream object so the file can be created by calling the constructor and having the smart pointer point to the newly constructed fstream object. Now when i run the program, I get something like:

"Unhandled exception at 0x01386b55 in TELE.exe: 0xC0000005: Access violation reading location 0x0000006c."

and the breakpoint points to this part of fstream (if statement):


_Myt *open(const char *_Filename,
ios_base::openmode _Mode,
int _Prot = (int)ios_base::_Openprot)
{ // open a C stream with specified mode
_Filet *_File;
if (_Myfile != 0 || (_File = _Fiopen(_Filename, _Mode, _Prot)) == 0)
return (0); // open failed
_Init(_File, _Openfl);
_Initcvt(&_USE(_Mysb::getloc(), _Cvt));
return (this); // open succeeded
}


Here's my code:


class CSVHandler
{
public:
CSVHandler();
CSVHandler( const std::string& filePath );
void SetFilePath( const std::string& filePath );
void Open();
void Close();
void NextCol();
void NextRow();

protected:

std::unique_ptr<std::fstream> _pFileHandler;
std::fstream _tempFileHandler;
std::string _filePath;
void CheckExtension();
std::string TempFilePath();

private:
};



void CSVHandler::Open()
{
_pFileHandler->open( _filePath, std::ios_base::in | std::ios_base::out | std::ios_base::ate );

// If file did not open successfully, terminate program
if( !_pFileHandler->is_open() )
{
// Maybe is doesn't yet exist, create file
_pFileHandler = std::unique_ptr<std::fstream>( new std::fstream( _filePath ) );
if( !_pFileHandler->is_open() )
{
std::cerr << "\nError: File could not be opened. \nExiting program...\n";
std::cin.get();
exit( 1 );
}
}
}


I must be doing something wrong with my smart pointer...

Share this post


Link to post
Share on other sites
Advertisement
Somewhat hard/impossible to tell without seeing if your constructor ever bothers to actually assign an fstream in the first place. I also have absolutely no idea why you believe it necessary to use a different fstream in the first place. If open fails, just call open again WITHOUT ios::in.

Share this post


Link to post
Share on other sites
...*smacks head* I don't know why I assumed leaving the pointer declaration in the class without actually giving it an fstream object in the constructor would do anything. Also now I know why my file wasn't being created. I was using open() with both ios::in and ios::out, or I was using open() with nothing but the file path, but not with just ios::open(). It seems I still don't have a full grasp on the modes. At least now I don't even have to worry about using a pointer. Thanks twofold. smile.png

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement