gpt4 book ai didi

c++ - 从文件中读取字符串。 QTextStream 不读取文件

转载 作者:行者123 更新时间:2023-11-28 07:23:11 29 4
gpt4 key购买 nike

我需要从 foo.txt 中读取字符串:

#include <QDebug>
#include <QFile>
#include <QStringList>
#include <QTextStream>

int main( int argc, char** argv )
{
QFile file("foo.txt");
if (!file.open(QIODevice::ReadOnly))
return 1;
QStringList stringList;
QTextStream textStream(&file);
textStream.readLine();
while (!textStream.atEnd())
stringList << textStream.readLine();

file.close();

qDebug() << stringList;

return 0;
}

outPut

文件已打开,但 textStream 始终为空。

最佳答案

从您的评论来看,可执行文件似乎根本找不到文件,因为它们位于不同的位置。有多种方法可以解决这个问题,这取决于最终用例的含义。以下是解决问题的一些方法:

  • 文件路径中的硬编码(相对或绝对)
  • 将文件移动到与可执行文件相同的目录
  • 使用命令行选项告诉可执行文件文件在哪里
  • 使用环境变量告诉可执行文件去哪里找

对于测试,前两个选项中的任何一个都是快速和简单的,但如果您打算更进一步,您可能会想要比这更好的东西。

关于c++ - 从文件中读取字符串。 QTextStream 不读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19145304/

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