I'm trying to read text with spaces from keyboard using object of QTextStream connected with stdin:
QTextStream cin(stdin, QIODevice::ReadOnly);
When I use >> it reads only till space.
QString str;
cin >> str;
readLine() reads until end of the line
str = cin.readLine()
But when readLine() used after >> it just skips and doesn't do second read. I suppose it because after >> it remains in the same line which is now empty.
cin >> str;
str = cin.readLine()
How can I force readLine() to perform second read? I can use empty readLine() to go to the next line, but maybe the are another solution?.
cin >> str;
cin.readLine()
str = cin.readLine()
Related
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
I'd like to monitorize how RAM memory I used for may Qt application; so, I thought about something to put inside the code.
I tried the following:
QProcess p;
p.start("ps -A");
p.waitForFinished();
QByteArray RamMem =p.readAllStandardOutput();
p.close();
quint16 pidcounter = 0;
QString pidString(RamMem);
QStringList RamMemSplit = pidString.split('\n');
quint16 RamMemSplitcounter = RamMemSplit.count();
while(pidcounter< RamMemSplitcounter)
{
if (RamMemSplit[pidcounter].contains(MyApp))
{
splitsplit = RamMemSplit[pidcounter].split(" ");
qDebug() << "Process:"<< splitsplit[10]<< "pid:"<< splitsplit[0];
}
pidcounter++;
}
In this way, I save name of running process associated to its pid;
Now, I'd like to apply another process ("pmax -x mypid), so I can obtain the RAM amount of my app:
How can I perform this? I read a QProcess requires a QStringList argument; in my case, I have only one parameter to use as argument and it's no so clear how to correctly set tyhe QProcess. The following is my idea, connected to the previous part:
QStringList listprova(splitsplit[0]);
QProcess pr;
pr.start("pmap -x", listprova);
pr.waitForFinished();
QByteArray pmapResult = pr.readAllStandardOutput();
pr.close();
QString pmapString(pmapResult);
QStringList pmapSplit = pmapString.split('\n');
quint8 pmapCounter = pmapSplit.count();
qDebug() << pmapSplit[pmapCounter]; // last line of "pmap -x [pid]" console command
You have 2 arguments, -x and the pid.
QStringList arguments;
arguments << "-x" << splitsplit[0];
QProcess pr;
pr.start("pmap", arguments);
I'm writing a unix cp program, but I'm unclear about checking for EOF. The code I have is:
int main(int argc, const char * argv[]) {
int in, out;
char buf[BUFFER_SIZE];
if (argc != 3)
cout << "Error: incorrect number of params" << endl;
if ((in = open(argv[1], O_RDONLY, 0666)) == -1)
cout << "Error: cannot open input file" << endl;
if ((out = open(argv[2], O_WRONLY | O_CREAT, 0666)) == -1)
cout << "Cannot create output file" << endl;
else
while ((read(in, buf, BUFFER_SIZE)) != -1)
write(out, buf, BUFFER_SIZE);
return 0;
}
It reads and writes fine, but writes past EOF when writing the output file. So I get a couple lines of gibberish past the end of the file. Am I just not checking for EOF correctly? I appreciate the input.
You should read the man page for the read function.
On end-of-file, read returns 0. It returns -1 only if there's an error.
read can read fewer bytes than you asked to (and it must do so if there aren't that many bytes remaining to be read). Your write call assumes that read actually read BUFFER_SIZE bytes.
You need to save the result returned by read and write only that many bytes -- and you need to terminate the loop when read returns 0 (indicating end-of-file) or -1 (indicating an error). In the latter case, you should probably do something to handle the error, or at least inform the user.
Incidentally, you don't need the 0666 mode argument when calling open to open the file for reading; that applies only with O_CREAT. Since open is actually a variadic function (like printf), you don't have to supply all the arguments.
The man page is not clear on this point; it pretends that there are two different forms of the open function:
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
but in fact that's not legal in C. The POSIX description correctly shows the declaration as:
int open(const char *path, int oflag, ...);
I need to parse the output of vol command only to get the id i.e only abcd-1234, that is being used in QProcess. Here is my code to get the Volume Serial Number:
QProcess process;
process.start("cmd /c vol C:");
process.waitForFinished(-1);
QByteArray out = process.readAllStandardOutput();
qDebug() << out;
Help me, thanks...
You can use the regular expression with QRegExp (http://qt-project.org/doc/qt-4.8/qregexp.html) to find the ID.
The vol command will always return the same message. So, you can read the result line by line and search the matching line:
QRegExp rx( "The Volume Serial Number is (.+)\\."); // Match the line with the ID and store it.
if ( rx.exactMatch( line ) {
QString id = rx.capturedTexts(1); // The first elt is the entire matching text.
qDebug() << id;
}
I am new to Qt and need to prepare a project to send hex commands from rs232.
QString line contains 64bit binary data which i have to convert into hexadecimal and send it through rs232 .
QString a=ui->comboBox->currentText();
QString s1;
s1="./calc "+a;
QProcess p1;
p1.start(s1);
p1.waitForFinished(-1);
QString line ;
//read
QFile file("TeleOutput.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in (&file);
line = in.readAll();
ui->plainTextEdit->setPlainText(line);
So, how to convert 64 bit binary data in QString line to hexadecimal value and transfer it through rs232?
First of all - you should really use QtSerialPort
Second of all - QString is a class, which works with actual string. QByteArray works with raw data. When you write QString line = in.readAll(); it implicitly calls QString(const QByteArray &ba), which uses QString::fromAscii.
Last of all, if you want to process 64bit integers, you should do something like this:
quint64 d;
QDataStream stream(&file);
while (!stream.atEnd())
{
stream >> d;
process(d);
}
Update
Quote:
My problem is that in plainTextEdit
"1111110101000101010101010101010101010101010101010101010......." 64
bit data is populated , i need to convert this data into hex and send it through rs232
Solution:
QString binData = plainTextEdit.toPlainText();
QByteArray result;
while (binData.size() >= 64)
{
quint64 d;
QString dataPiece = binData.left(64);
binData.remove(0, 64);
d = dataPiece.toULongLong(0, 2);
result += QByteArray::number(d);
}
_com->write(result);
_com->flush();
Where _com is a pointer to QtSerialPort, with all parameters set and opened without errors.