gpt4 book ai didi

c++ - 为什么qt不读取文件

转载 作者:行者123 更新时间:2023-11-30 02:30:33 24 4
gpt4 key购买 nike

我有以下几行代码:

QFile file("1.txt");
qDebug() << file.readAll();

该文件位于项目目录中。当我编译代码时,我得到:

"QIODevice::read (QFile, "1.txt"): device not open"

最佳答案

您必须在阅读前打开文件。

作为起点(来自文档):

QFile file("in.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;

while (!file.atEnd()) {
QByteArray line = file.readLine();
process_line(line);
}

可以找到其他选项here .

关于c++ - 为什么qt不读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38502468/

24 4 0
文章推荐: java - 为什么不能执行 Set = new TreeSet