0
votes

I have this code:

std::string ff = "C:\\res\\pp.txt";
std::ifstream test(ff);
if(test.is_open())
{
    std::string s;
    std::getline(test, s);

}

If I put a breakpoint at the construction and analize the test object, it is constructed but the buffer is an incorrect pointer. Then, getline crashes because test is corrupted.

As a side information, this code is executed into a project that links with some libraries as gameplay3d.

Some ideas?

EDIT: This is the stack trace:

msvcp100d.dll!std::_Xfiopen(const char * filename, int mode, int prot) Línea 85 C++ msvcp100d.dll!std::_Fiopen(const char * filename, int mode, int prot) Línea 94 + 0x11 Bytes C++ sample-browser.exe!std::basic_filebuf >::open(const char * _Filename, int _Mode, int _Prot) Línea 220 + 0x1d Bytes C++ sample-browser.exe!std::basic_ifstream >::basic_ifstream >(const std::basic_string,std::allocator > & _Str, int _Mode, int _Prot) Línea 725 + 0x1f Bytes C++ sample-browser.exe!Audio3DSample::initialize() Línea 30 + 0x15 Bytes C++

All seems to be ok until code hits _Xfiopen. At this moment, fp is not created and, although ifstream is created, the internal filebuffer is null. If I put this code in another project it works, and more importantly, the code that constructor calls and the stack is totally different.

2
I see nothing wrong with what little code is here (using is_open rather than just if (test) aside), and it is obviously testable as valid when isolated in a stand-alone main() program. Which means your problem is somewhere else. - WhozCraig
Does it still fail if you instead check with if(test.is_open() and test.good()) - Nikos C.
Yes, it still fails using test.good() - Killrazor
@WhozCraig I agree with you that error must be at somewhere else. The code, separated in an isolated project works, but I'd like to know what king of code could make fail an stl code. I have debugged the ifstream constructor and the interna fopen simply return a null pointer, so I think that there is an awful memory corruption. - Killrazor
@Killrazor What does GetLastError() return after the constructor? On Visual Studio, there is a special pseudo-register called @err, that shows GetLastError() text. - sashoalm

2 Answers

2
votes

Your description of the problem sounds as if you are using headers for one standard C++ library but the implementation from another, incompatible implementation. In many cases things are setup such that there should be a link failure but it can't be prevented that incompatible libraries are linked in all cases. I would verify that the headers match the actual implementation.

0
votes

First don't pass string in ifstream, make it to const char* type.

 std::ifstream test(ff.c_str());