由于QTextStream的无限循环
Infinite cycle due to QTextStream
所以,我在尝试从文件中读取行(逐行(时得到无限循环。我试图像这样使用do{}while();
循环:
QTextStream stream(stdin);
QString line;
do {
line = stream.readLine();
} while (!line.isNull());
但我得到空字符串。
当然,我检查了文件路径(是正确的(。我试图使用/Users/user/tts.txt
路径但没有更改。我试图读取其他文件(如 m3u(。而且它不适用于macOS Catalina,Windows 10,Linux(Debian(。
那么,为什么我会得到无限循环呢?
QStringList Manager::GetLinesFromFile(const QString &nameOfFile)
{
QStringList lines = {};
//path to file
const QString path = QCoreApplication::applicationDirPath() + "/bin/" + "tts.txt";
//"/Users/user/tts.txt"
QFile buffer;
buffer.QFile::setFileName(path);
#ifndef Q_DEBUG
qDebug() << path;
#endif
if(buffer.QFile::exists())
{
if(!buffer.QIODevice::open(QIODevice::ReadOnly))
{
#ifndef Q_DEBUG
qCritical() << "error: can't open file";
#endif
}
else
{
QTextStream stream(&buffer);
// both conditions
// (!stream.QTextStream::atEnd())
while(!buffer.QFileDevice::atEnd())
lines.QList::push_back(stream.QTextStream::readLine());
buffer.QFile::close();
}
}
else
{
#ifndef Q_DEBUG
qCritical() << "error: file not exists";
#endif
}
return lines;
}
查看 QTextstream 文档 https://doc.qt.io/qt-5/qtextstream.html。有一个逐行阅读的示例。你的 while 循环应该读取,直到流到达缓冲区的末尾,并且当他的发生时,许多内置的读取函数将返回 false
所以,我明白了。我错误地打开了文件。
我正在使用:
if(!file.QIODevice::open(QIODevice::ReadOnly))
但它应该是这样的:
if(!file.QFile::open(QFile::ReadOnly))
相关文章:
- 过载'operator new'如何导致无限循环?
- C++正则表达式无限循环
- 程序在尝试猜测它选择的随机数时进入无限循环?
- 遍历链表时的无限循环
- 循环链表:无限循环
- 比较两个字符串后卡在无限循环中
- 在做一段时间内检查字符的无限循环
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- 可能的无限循环
- 超出 int 的值范围后的无限循环
- 为什么这段代码会导致无限循环?
- 运行无限循环的最小二叉树问题
- 在分配和发布递增循环迭代器时C++无限循环(gcc 错误?
- 连接线程时的无限循环
- 为什么这种方法会进入无限循环?
- 由于QTextStream的无限循环
- Qt 在另一个线程中无限循环
- 从文件 C++ 读取时的无限循环
- 试图找出为什么我的代码保持无限循环