gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:59:09 26 4
gpt4 key购买 nike

方法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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com