Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在windows上使用std::stringstream时发生访问冲突_C++_File_Stringstream - Fatal编程技术网

C++ 在windows上使用std::stringstream时发生访问冲突

C++ 在windows上使用std::stringstream时发生访问冲突,c++,file,stringstream,C++,File,Stringstream,我目前正在实现一个基本的文件加载函数。使用std::stringstream时,程序在stringstream析构函数中发生访问冲突而崩溃。以下是函数: void macro_storage_t::load(std::string filename) { std::ifstream file(filename); if (file.is_open()) { clear(); char line_c[4096]; while

我目前正在实现一个基本的文件加载函数。使用
std::stringstream
时,程序在
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);

            }
        }
    }
}
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();
    }
}
并使用此功能保存:

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);

            }
        }
    }
}
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();
    }
}
stringstream
被隐式删除时,就会发生错误

我在Windows 7上使用Visual Studio 2010。

请尝试以下方法:

void macro_storage_t::load(std::string filename)
{
    std::ifstream file(filename);
    if (file.is_open())
    {
        clear();

        std::string line;
        if (std::getline(file, line))
        {       
            if (line.substr(0, 8) == "VERSION ")
            {
                // optional: read the actual version number from the line,
                // if it affects how the following values must be read...

                while (std::getline(file, line))
                {
                    std::istringstream ss(line);
                    int a, b, c, d;

                    if (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);

                        entry_list_.push_back(entry);
                    }
                }
            }
        }
    }
}
void宏存储\u t::load(std::string文件名)
{
std::ifstream文件(文件名);
if(file.is_open())
{
清除();
std::字符串行;
if(std::getline(文件,行))
{       
if(第0、8行)=“版本”)
{
//可选:从行中读取实际版本号,
//如果它影响必须读取以下值的方式。。。
while(std::getline(文件,行))
{
std::istringstream ss(线路);
INTA、b、c、d;
如果(ss>>a>>b>>c>>d)
{
进入(t)进入;;
entry.timestamp=a;
entry.type=静态(b);
entry.button=静态_转换(c);
entry.key=静态(d);
条目\列表\推回(条目);
}
}
}
}
}
}

为什么不直接读入
并跳过第c行,
std::getline(std::cin,line)
?在调试器中运行时会发生什么情况?“我现在正在调试这个问题15分钟…”我不确定这是否可以在堆栈溢出时有效地提问。通常情况下,在来到这里之前,你需要付出更多的努力。15分钟调试一个问题不算什么——尝试几个小时、几天、几周甚至几个月来解决真正棘手的问题。@πάνταῥεῖ 啊,那我想我该回家了。我想如果你有问题的话来stackoverflow是可以的,但是谢谢你清理这些东西。。。我自己找不到解决这个问题的办法。我在谷歌上搜索并尝试过,但从未遇到过这个错误。在linux中,它运行得很好…那么你可能是安装不好,或者编译器生成了不好的codegen。你知道我能做些什么吗?@RemyLebeau在他编写的原始代码中
if(line.find(“VERSION”)==std::string::npos)
但您的代码会查找以版本开头的行,而不是不包含版本的行。@Nidhoegger您可能在程序的其他地方有未定义的行为。使用诸如
valgrind
@kfsone之类的内存调试工具:原始代码在读取的每一行上查找
“版本”
。如果在一行上找不到,则假定该行是(但未验证为)4-int行。但是只有第一行会有
“VERSION”
,所以我的代码只检查第一行。如果在第一行中找到了
“VERSION”
,则假定(并验证)所有后续行为4-int行,否则不读取它们。这样,由于没有反复检查
“版本”
,因此使用的开销更少。