I am currently implementing a rudimentary file load function. When using a std::stringstream
the program crashes with a access violation in the stringstream
destructor. Here is the function:
void macro_storage_t::load(std::string filename)
{
std::ifstream file(filename);
if (file.is_open())
{
clear();
char line_c[4096];
while (file.getline(line_c, 4096))
{
std::string line(line_c);
if (line.find("VERSION") == std::string::npos)
{
std::stringstream ss(std::stringstream::in | std::stringstream::out);
int a, b, c, d;
ss << line;
ss >> a >> b >> c >> d;
entry_t entry;
entry.timestamp = a;
entry.type = static_cast<entry_type_t>(b);
entry.button = static_cast<button_t>(c);
entry.key = static_cast<BYTE>(d);
}
}
}
}
The file it loads looks like this (shortened for better readability):
VERSION 1
0 14 254 0
and is saved with this function:
void macro_storage_t::save(std::string filename)
{
std::ofstream file(filename, std::ios::trunc);
if (file.is_open())
{
file << "VERSION " << MACRO_VERSION << std::endl;
for (std::vector<entry_t>::iterator it = entry_list_.begin(); it != entry_list_.end(); ++it)
{
entry_t entry = *it;
file << (int)entry.timestamp << " " << (int)entry.type << " " << (int)entry.button << " " << (int)entry.key << std::endl;
}
file.close();
}
}
The error is:
Unhandled exception at 0x0f99a9ee (msvcp100d.dll) in FLAP.exe: 0xC0000005: Access violation reading location 0x00000004.
The error happens as soon as the stringstream
gets deleted implicitly...
I use Visual Studio 2010 on Windows 7.