Qt - QFile - How to read only first word in every line - qt

How can I read only first word in every line in a text file while using QFile in Qt?
Thanks.

use
QFile ifile("in.txt");
QString text = txStream.readLine();
QStringList splitline = text.split(" ");
QFile ofile("out.txt");
ofile.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&ofile);
// join QStringList by "\n" to write each single word in an own line
out << splitline.join("\n");
ofile.close();

Related

How to enter input separated by semi colon by using setInputMask()

My goal is to display only files which user wants to see. For instance, "*.h; *.txt" as an input should shows the only *.h and *.txt files in selected folder. The program works for only one input(ie. *.h) The code:
QString mask = ";";
QStringList stringList(ui->lineEdit->text());
ui->lineEdit->setInputMask(mask);
QFileInfoList fileList = qdir.entryInfoList(QStringList() << stringList, QDir::Files, QDir::Size);
The program display when the users enter input only one type of file:
But it does not display when the users enter input as *.h; *.cpp
Best regards!
I solved it.
All I need to do was
QString str = ui->lineEdit->text();
QStringList stringList = str.split(QLatin1Char(';'));
QFileInfoList fileList = qdir.entryInfoList(QStringList() << stringList, QDir::Files, QDir::Size);

How can I get required line?

I wrote code, that takes output from QProcess to QTextStream, than show line that I need:
QProcess p;
p.start("fdisk",QStringList() << "/dev/sdb" << "-l");
p.waitForFinished();
QString processOutput = p.readAll();
QTextStream processOutputTextStream(&processOutput);
QString line;
while(!processOutputTextStream.atEnd()){
line = processOutputTextStream.readLine();
if(line.contains("Disk /dev/sdb:")){
qDebug() << line;
}
}
The output:
"Disk /dev/sdb: 28.67 GiB, 30765219840 bytes, 60088320 sectors"
I need only last numbers (60088320)
How to do this?
auto words = line.split(" "); auto number_str = words.at(words.length() - 2);
Thanks to eyllanesc

Overwrite an existing file using Qt

I'm trying to overwrite an existing file using Qt. Since QFileDialog::getSaveFileName will always replace the selected file if it already exists, I've tried creating a custom QFileDialog in over to get to my goal so I used what has been suggested in this link but it seems not to work...
What I've tried so far:
QFileDialog diag(this);
diag.setFileMode(QFileDialog::AnyFile);
diag.setNameFilter(tr("Test (*.tst)"));
diag.setAcceptMode(QFileDialog::AcceptSave);
diag.exec();
QStringList namefile = diag.selectedFiles();
QFile file(namefile[0]);
if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
QTextStream stream(&file);
stream << ui->lineEdit->text() << endl;
}
file.close();
result:

Convert QTextStream to QByteArray

I need to convert a QTextStream to a QByteArray, and then back again. I found an example of QTextStream -> QByteArray by constructing a QTextStream(QBytearray) and then any text < < to the stream ends up in the bytearray.
But how about the other way? Probably a one liner but I can figure it out. Can someone post and explain?
Check it out if your text stream works via file (maybe via socket):
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
QString text;
text = in.readAll();
file.close();
text.QString::toUtf8(); //convert your data to byte array
To get your data back use: QString::fromUtf8(const QByteArray &str)

How to make a QString from a QTextStream?

Will this work?
QString bozo;
QFile filevar("sometextfile.txt");
QTextStream in(&filevar);
while(!in.atEnd()) {
QString line = in.readLine();
bozo = bozo + line;
}
filevar.close();
Will bozo be the entirety of sometextfile.txt?
Why even read line by line? You could optimize it a little more and reduce unnecessary re-allocations of the string as you add lines to it:
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
QTextStream in(&file);
QString text;
text = in.readAll();
file.close();
As ddriver mentions, you should first open the file using file.open(…); Other than that, yes bozo will contain the entirety of the file using the code you have.
One thing to note in ddriver's code is that text.reserve(file.size()); is unnecessary because on the following line:
text = in.readAll();
This will replace text with a new string so the call to text.reserve(file.size()); would have just done unused work.

Resources