我有这几行代码:
QFile file("h:/test.txt");
file.open(QFile::ReadOnly | QFile::Text);
QTextStream in(&file);
bool found = false;
uint pos = 0;
do {
QString temp = in.readLine();
int p = temp.indexOf("something");
if (p < 0) {
pos += temp.length() + 1;
} else {
pos += p;
found = true;
}
} while (!found && !in.atEnd());
in.seek(0);
QString text = in.read(pos);
cout << text.toStdString() << endl;
Run Code Online (Sandbox Code Playgroud)
我们的想法是在文本文件中搜索特定的char序列,并在找到时将文件从开头加载到搜索文本的出现位置.我用于测试的输入是:
this is line one, the first line
this is line two, it is second
this is the third …Run Code Online (Sandbox Code Playgroud) 我想查看读取QFile的QTextStream的下一个字符,以便创建一个有效的标记器.
但是,我没有找到任何令人满意的解决方案.
QFile f("test.txt");
f.open(QIODevice::WriteOnly);
f.write("Hello world\nHello universe\n");
f.close();
f.open(QIODevice::ReadOnly);
QTextStream s(&f);
int i = 0;
while (!s.atEnd()) {
++i;
qDebug() << "Peek" << i << s.device()->peek(3);
QString v;
s >> v;
qDebug() << "Word" << i << v;
}
Run Code Online (Sandbox Code Playgroud)
给出以下输出:
Peek 1 "Hel" # it works only the first time
Word 1 "Hello"
Peek 2 ""
Word 2 "world"
Peek 3 ""
Word 3 "Hello"
Peek 4 ""
Word 4 "universe"
Peek 5 ""
Word 5 ""
Run Code Online (Sandbox Code Playgroud)
我尝试了几个实现,也使用了QTextStream :: …
到目前为止,我使用qDebug().noquote(). 这很容易,因为它只需要一个简单的#import <QDebug>
现在我需要将所有内容输出到stdout,但我不知道如何轻松完成。我是这样被教导的:
QTextStream cout(stdout, QIODevice::WriteOnly);
Run Code Online (Sandbox Code Playgroud)
然而,创建一个新对象比一个简单的#import <QDebug>. stdout在qt中处理的好/最不麻烦的方法是什么?
这是一个非常简单的应用程序来说明我遇到的问题。
#include <QTextStream>
int main()
{
QTextStream cin(stdin);
QTextStream cout(stdout);
QString test;
cout << "Enter a value: ";
cout.flush();
cin >> test;
cout << "Enter another value: ";
cout.flush();
test = cin.readLine();
cout << test;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望执行暂停并等待输入test = cin.readline();,但事实并非如此。如果我删除cin >> test;然后它暂停。
为什么这段代码的行为是这样的,我如何获得我想要的行为?
我的问题很简单,但我无法解决这个问题......
在我的标题中:
QTextStream *in = NULL;
Run Code Online (Sandbox Code Playgroud)
在一个方法中,QTextStream被初始化:
in = new QTextStream(&file);
Run Code Online (Sandbox Code Playgroud)
然后我试图用另一种方法解析它:
QString next;
if(in != NULL){
while(!in->atEnd()){
next = in->readLine();
}
}
else{
QMessageBox::critical(this, "Error", "No file to test!");
}
Run Code Online (Sandbox Code Playgroud)
当初始化精品工程,应用程序崩溃的测试,如果在为atEnd() .我究竟做错了什么?我需要的是可访问的几种方法.我要在这里(?)使用指针,因为在后来被初始化(据我所知这是不可能的引用)
这可能是显而易见的,但我对c ++相当新...
谢谢!
这是我的代码:
void MainWindow::save(){
QString fileName = QFileDialog::getSaveFileName(this, tr("Save Text File"), path, tr("Text Files (*.txt)"));
if (fileName != "")
{
QFileInfo info(fileName);
path = info.absoluteDir().absolutePath();
QFile file(path);
if(!file.open(QIODevice::WriteOnly)){
QString text = ui->plainTextEdit->toPlainText();
QTextStream out(&file);
out << text ;
file.close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是在单击pushButton后它不会创建任何.txt文件:
connect(ui->saveButton, SIGNAL(clicked(bool)), this, SLOT(save()));
Run Code Online (Sandbox Code Playgroud)
日志:
QIODevice::write (QFile, "C:\Users\kfg\Desktop"): device not open
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助 :)
qtextstream ×6
c++ ×5
qt ×5
peek ×1
qdebug ×1
qfile ×1
qfiledialog ×1
qt4 ×1
seek ×1
stdout ×1