在Release-Build中读取File时,Debug Assertion失败

时间:2017-10-15 17:42:35

标签: c++ file build

方法readFile应该将文件的所有字符读入字符串并返回它。打开后,打开状态将打印到控制台。当我编译Debug-Build时,返回的字符串与文件的内容相匹配。但是当我编译Release-Build时,带有getline的行会抛出Debug Assertion失败并显示一个弹出窗口,其中包含以下文本:

Debug Assertion失败!

文件:minkernel \ crts \ ucrt \ src \ appcrt \ lowio \ read.cpp

表达式:_osfile(fh)& FOPEN

CODE:

#include <string>
#include <iostream>
#include <fstream>

using namespace std;

string readFile(const string& fileName) {
    cout << '\'' << fileName << '\'' << endl;
    ifstream file{ fileName };

    cout << file.is_open() << endl;

    string str;
    string content;
    cout << "START" << endl;

    while (getline(file, str)) {
        cout << "READ" << endl;
        content += str;
        content.push_back('\n');
    }

    file.close();

    return content;
}

1 个答案:

答案 0 :(得分:0)

我只是遇到了同样的问题,并且在这里找到了解决方案:Dan Nissenbaum's answer

我所做的只是将运行时库链接模式更改为/ MTd。您可以在项目设置->配置属性-> C / C ++->代码生成->运行时库中更改它