正在将有效的 QFile 转换为 QString - QString 为空
Converting valid QFile to QString - QString is empty
所以我正在尝试通过执行以下操作将QFile转换为QString:
void MainWindow::openTemplateFile(QString location)
{
if (location.isEmpty())
return;
else
{
QString variable;
templateFile.setFileName(location);
if (!templateFile.open(QFile::ReadOnly | QFile::Text))
{
QMessageBox::information(this, "Unable to open template",
templateFile.errorString());
return;
}
else // file opened and ready to read from
{
QTextStream in(&templateFile);
QString fileText = in.readAll();
qDebug() << templateFile.size() << in.readAll();
}
}
}
但是,在调试控制台中我得到以下结果:
48 ""
templateFile 确实存在,并且是 MainWindow 类的一部分。这也是简化的代码 - 在实际程序中,我从文件中读取字符并且它工作正常。位置字符串是 QFileDialog::getOpenFileName 函数的结果,我用它来打开一个 txt 文件。
你给readAll()
打了两次电话。第二次,流位于文件末尾,因此readAll()
没有要读取的内容并返回一个空字符串。改为在调试输出中打印fileText
。
相关文章:
- initializer_list中字符串的内容为何为空
- 复制几乎为空的数组的最快方法
- 为什么除了继承聚合结构之外,结构为空,无法聚合初始化?
- 堆分配的对象是否存在永不为空的唯一所有者?
- 从封装在对象中的函数 C++ 返回时为空的列表
- 如何检查类实例向量的索引是否为空
- 为什么当 vector 为空时会显示运行时错误?
- 提升精神 x3 解析为结构,如果它为空,则跳过成员
- 为什么成员函数内的"this"指针为空?
- 标准是否保证无捕获的 lambda 为空?
- 变量始终为空
- 节点是否为空,即使它有一个值?
- 为什么我的 Char* 在填充整数后为空?
- C++库和自注册类:客户端应用程序中的工厂映射为空
- 尽管直接设置了指针数组,但仍为空
- 如何测试一行是否为空?
- C++中的嵌套初始值设定项列表为空
- C++:如果括号为空,为什么不抛出错误对象?
- 值已更改(常量 Qstring &) 在 qspinbox 中为空时不触发
- 正在将有效的 QFile 转换为 QString - QString 为空