【问题标题】:Debug Assertion fails while reading File in Release-Build在 Release-Build 中读取文件时调试断言失败
【发布时间】:2017-10-15 17:42:35
【问题描述】:

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

调试断言失败!

文件: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;
}

【问题讨论】:

  • 发布可编译代码。
  • @NeilButterworth 完成
  • 好吧,这个:cout &lt;&lt; '\'' &lt;&lt; fileName &lt;&lt; '\'' &lt;&lt; endl; 是错误的,假设您尝试添加反斜杠 - 它会添加引号。
  • 它只是在引号中打印文件的路径,以确保它是正确的并且不以空格开头或结尾。

标签: c++ file build


【解决方案1】:

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

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

【讨论】:

  • 哈哈谢谢。我不知道我还有这个问题:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-27
  • 1970-01-01
相关资源
最近更新 更多