【发布时间】: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 << '\'' << fileName << '\'' << endl;是错误的,假设您尝试添加反斜杠 - 它会添加引号。 -
它只是在引号中打印文件的路径,以确保它是正确的并且不以空格开头或结尾。