c++ - 在发布版本中读取文件时,Debug断言失败

标签 c++ file build

方法readFile应该将文件的所有字符读入字符串并返回。打开后,打开状态将打印到控制台。当我编译Debug-Build时,返回的字符串与文件的内容匹配。但是,当我编译发布版本时,带有getline的行会引发Debug Assertion失败,并显示带有以下文本的弹出窗口:
调试断言失败!
文件:minkernel \ crts \ ucrt \ src \ appcrt \ lowio \ read.cpp
表达式:_osfile(fh)和FOPEN
码:

#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;
}

最佳答案

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

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

关于c++ - 在发布版本中读取文件时,Debug断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46758077/

相关文章:

angular - 将 MathJax 集成到 SystemJS 构建中

android - Gradle说onStartCommand的return语句无法到达

linux - 如何避免在执行 bash 脚本期间出现错误消息?

java - 使用 Zk 框架下载 zip

c - 写入文件时为"Access violation writing location"

scala - 如何构建独立的 scala 可执行文件

c++ - 在派生类中使用基类的模板参数

c++ - 我的程序在 Windows 上使用 MSVC 和 g++ 编译器运行,但在 Linux 上不运行

c++ - 弯路捕捉质感

php - 如何查询简单机械论坛的主题流行度/评级