我目前正在實現一個基本的文件加載函數。當使用std::stringstream
時,該程序在stringstream
析構函數中發生訪問衝突時崩潰。下面是函數:在windows上使用std :: stringstream時的訪問衝突
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);
}
}
}
}
它加載的文件看起來像這樣(縮短了更好的可讀性):
VERSION 1
0 14 254 0
,並保存有這樣的功能:
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();
}
}
的錯誤是:
Unhandled exception at 0x0f99a9ee (msvcp100d.dll) in FLAP.exe: 0xC0000005: Access violation reading location 0x00000004.
錯誤一旦被stringstream
隱含刪除後會出現...
我使用Visual Studio 2010在Windows 7
你爲什麼不讀入'線'直接跳過line_c,'std :: getline(std :: cin,line)'? – kfsone
在調試器中運行它會發生什麼? – kfsone
_「我現在正在調試這個問題15分鐘......」_我不確定這是否有效在Stack Overflow中提出問題。通常你會在你來這裏之前花更多的精力。 –