- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试读取文本文件并在 QPlainTextEdit 中显示内容。请你指出我做错了什么:
QFile jsonFile("data.json");
if (!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << "Failed to open file";
qDebug() << jsonFile.errorString();
return;
}
else
{
qDebug() << "File opened";
} //It returns that the file opened successfully
qDebug() << "File Exists?: " << jsonFile.exists(); //Yep, it exists.
QTextStream outStream(&jsonFile);
QString textString = outStream.readAll();
qDebug() << "Text string: " << textString; //textString is empty! ""
ui->fileToPost->setPlainText(textString); //fileToPost is the QPlainTextEdit
jsonFile.close();
如果我做类似的事情
QString textString = "The cat sat on the mat";
它显示正常。问题是没有从流(或文件)中读取任何内容。
最佳答案
尝试检查文件的绝对路径,可能不在您期望的位置:qDebug()<<QFileInfo("data.json").absoluteFilePath();
关于c++ - Qt 从 QTextStream 读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17676862/
我一直在玩一些 c++ hello world 的东西,并认为我会快速获得 qt 字符串。回显和转换字符串很好,但是 endl似乎很特别: #include int main(int, char*[
我想查看读取 QFile 的 QTextStream 的下一个字符,以创建一个高效的标记器。 但是,我没有找到任何令人满意的解决方案。 QFile f("test.txt"); f.open(QIOD
如何在标题中设置一个新的 QTextStream,就像这样 MainClass{ private: QTextStream out; } 然后,在初始化设置文件,而不是 QTextStream
我正在打开一个文件并获取它的 QTextStream。然后我使用 readLine() 逐行读取流。当该行与某个字符串匹配时,我需要将其替换为另一个字符串。我需要的行为是完全替换该行(即,如果该行是“
我正在学习 QtCreator,但在向文件中写入数据时遇到了问题。这是一些代码: char *str; int i = 1; QFile outFile("path/to/file/ou
我只是关注这个主题“How to print to console when using Qt”,以使我的应用程序正常运行。我开始怀疑 - 是否可以为单个 QTextStream 订阅更多输出?例如,
我尝试从同一个文件读取和写入,但出现奇怪的行为。这是示例代码: 主窗口.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include #include
我的问题很简单,但我无法解决... 在我的标题中: QTextStream *in = NULL; 在一种方法中,QTextStream 被初始化: in = new QTextStream(&fil
我在学习 Qt,使用 Qt5.1,我在重音方面有编码问题。我用葡萄牙语编写带有输出的程序,并且总是必须使用重音符号。该问题可以在以下代码中演示: \#include \#include \#inc
我正在为文本文件编写解析器,但 QTextStream 似乎缺少某些内容。我使用的是 Qt 5.4.1,代码是单线程的,如果这很重要的话。 这是方法: const Event* AviLogFile:
我有这几行代码: QFile file("h:/test.txt"); file.open(QFile::ReadOnly | QFile::Text); QTextStream in(&file);
我正在尝试使用 QTextStream 来格式化数字输出。我已将我的问题简化为一个小测试程序来演示我的问题。以下是我的代码: #include #include #include int mai
我的文件名中有空格,但由制表符分隔。我如何使用 QTextStream 一个一个地阅读它们? 正常的方式会被制表符和空格分割(实际上是任何 QChar::isSpace()),这不是我想要的: QSt
我正在使用以下代码将一些文本写入文件: QFile caFile(outputFolder + "file.extension"); caFile.open(QIODevice::WriteOnly
我想在 linux 机器上读取和解析/proc/PID/status 文件的内容,但 QTextStream.atEnd 在开始读取时总是返回 true。编码: QString procDirecto
我正在使用 QTextStream 将数据写入文件。它组织得很好(几乎完美),例如: i t y yy 0 0.016
我使用以下代码查找从 QFile 中读取的字节数。对于某些文件,它给出了正确的文件大小,但对于某些文件,它给出了一个近似于 fileCSV.size()/2 的值。我正在发送两个文件,其中的字符数相同
readLine()后,如何设置光标位置到行首? 使用 seek() 和 pos() 对我不起作用。 这是我的 file.txt 的样子: Object1 Some-name 2 3.40 1.50
我正在使用 QTextStream 写入 QFile,一切正常。我正在尝试创建一些错误检测,因此我尝试删除写入之间的输出文件。 扼杀,Qtextstream的状态继续显示0(无错误),QFile的er
我需要从 foo.txt 中读取字符串: #include #include #include #include int main( int argc, char** argv ) {
我是一名优秀的程序员,十分优秀!