Jump to content

  • Log In with Google      Sign In   
  • Create Account


unique_ptr and fstream constructor problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Robot Ninja   Members   -  Reputation: 569

Like
0Likes
Like

Posted 06 March 2012 - 10:14 PM

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...

Sponsor:

#2 Trienco   Crossbones+   -  Reputation: 2085

Like
1Likes
Like

Posted 06 March 2012 - 11:42 PM

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.
f@dzhttp://festini.device-zero.de

#3 Robot Ninja   Members   -  Reputation: 569

Like
0Likes
Like

Posted 07 March 2012 - 12:08 AM

...*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. Posted Image




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS