开始从文件读取时,QTextStream atEnd()返回true(QTextStream atEnd() is returning true when starting to read from

编程入门 行业动态 更新时间:2024-10-24 04:39:56
开始从文件读取时,QTextStream atEnd()返回true(QTextStream atEnd() is returning true when starting to read from a file)

我想在linux机器上读取和解析/ proc / PID / status文件的内容,但是QTextStream.atEnd在开始读取时总是返回true。 代码:

QString procDirectory = "/proc/"; procDirectory.append(QString::number(PID)); procDirectory.append("/status"); QFile inputFile(procDirectory); if (inputFile.open(QIODevice::ReadOnly)) { QTextStream in(&inputFile); QString line; while (!in.atEnd()) { line = in.readLine();

文件存在,如果我在没有while表达式的情况下手动读取行,则会正常读取文件。

我错过了一些明显的东西吗

(Debian 8 x64,QT 5.4.1 x64,gcc 4.9.2)

I want to read and parse contents of the /proc/PID/status file on a linux machine, but the QTextStream.atEnd is always returning true when starting to read. The code:

QString procDirectory = "/proc/"; procDirectory.append(QString::number(PID)); procDirectory.append("/status"); QFile inputFile(procDirectory); if (inputFile.open(QIODevice::ReadOnly)) { QTextStream in(&inputFile); QString line; while (!in.atEnd()) { line = in.readLine();

File exists and if I read lines manually without the while expression, the files are read normally.

Did I miss something obvious?

(Debian 8 x64, QT 5.4.1 x64, gcc 4.9.2)

最满意答案

循环遍历这些流的首选方法是使用do / while循环。 这是为了允许流在进行任何查询(如atEnd)之前正确检测Unicode。

QTextStream stream(stdin); QString line; do { line = stream.readLine(); } while (!line.isNull());

The preferred way oft looping over these streams is with a do/while loop. This is for allowing the stream to detect Unicode correctly before any queries (like atEnd) are made.

QTextStream stream(stdin); QString line; do { line = stream.readLine(); } while (!line.isNull());

更多推荐

本文发布于:2023-04-27 23:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329079.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   atEnd   QTextStream   true   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!