I need to run following statement from QProcess:
programm < file1 > file2
in QT:
QProcess *proc = new QProcess;
proc->setReadChannelMode(QProcess::SeparateChannels);
proc->start("program < \"file1\" > \"file2\"", QIODevice::ReadWrite);
But somehow it does not work. I see in taskmanager, that the command looks correctly, but it seems as the program is executed without any arguments. Where is my error?
Reading from and writing into files using < respectively > is a syntax feature of the shell. If you run the command line programm < file1 > file2 using a shell like sh, the command program gets executed only, with no arguments at all. Assigning the programs channels for input and output to the given files doesn't have anything to do with the command itself.
But QProcess can be told to simulate this behaviour by using these methods:
QProcess::setStandardInputFile(QString fileName)
QProcess::setStandardOutputFile(QString fileName)
So your code becomes:
QProcess *proc = new QProcess;
proc->setReadChannelMode(QProcess::SeparateChannels);
proc->setStandardInputFile("file1");
proc->setStandardOutputFile("file2");
proc->start("program");
Related
Say, I have a program called "tree". It takes as input a text file containing some dictionary of English words, call it engDict.txt.
In my terminal, I would execute this program using the following command:
>> ./tree engDict.txt
Assuming this program automatically reads in the file and processed it. Then, it prompts the user: Would you like to exit the program (y/n)?
Now, my question is:
How can I send input to this program in one line?
For example, I tried the following, but it doesn't do what I'd like.
>> ./tree engDict.txt | n
or
>> ./tree engDict.txt ; n
Close. The letter n should be written to the input.
echo n | ./tree engDict.txt
I wrote a very simple code using qtcreator It's a console application and I want to set up input and output text file, so qtcreator get's inputs form input file(not console) and save the output in output file is there any kind of option in qtcreator that can specify input and output file
If your program is already working by reading from console and writing to console using stdin and stdout, you just have to use console redirects.
You need to start your program from a command line using :
echo input.txt | myprogram.exe > output.txt
(windows)
cat input.txt | myprogram > output.txt
(unix)
Unfortunately, there is no possibility to redirect stdin and stdout from Qt Creator run options, AFAIK. You have to start the program yourself from the command line
You may try this for C++.
stringstream fcin(R"(1
3
1 2 3)");
istream cin(fcin.rdbuf());
int n;
while (cin >> n) {
cout << n << " ";
}
I am using QT in a windows (do not know if that matters) application and want to start some process from my application using QProcess.
(actually through a qtscript wrapper that uses QProcess)
This seems to work but i have problems when using more advanced command lines like connecting programms using pipes.
If i start a process using the following program lines:
QProcess proc;
QString command = "grep \"false negatives\" test.txt | cut -f2";
proc.start(command);
The grep command complains that it could not find the file "2". so obviously the command line is not interpreted as i would expect it to do.
if i prefix a cmd /C to the command it works well but this obviously is not OS independent anymore and may have some additional caveats regarding command line parameter.
Is there any Qt like way to handle that and force Qt to use some default command line interpreter?
Is there any Qt like way to handle that and force Qt to use some default command line interpreter?
The simple answer is no, there isn't a Qt default command line interpreter
QString command = "grep \"false negatives\" test.txt | cut -f2";
This command doesn't work because QProcess takes the first token (grep) and uses that as the command, then passes each item, separated by a space to that command. In this case, the pipe command is not a valid argument for grep and neither is cut, nor -f2.
I commented that the answer to this question was possibly similar, as it demonstrates how you can successfully use the pipe command with QProcess; note that the arguments are surrounded by quotes.
As you don't want to call cmd or a *nix equivalent such as bash, you can handle this with two calls to QProcess; the first for the grep command and the 2nd for the cut, passing in the output from the first QProcess call.
The function QProcess::setStandardOutputProcess makes this easier, allowing you to create the pipe directly between the two QProcess objects.
Therefore you'd do something like this: -
QProcess proc1;
QProcess proc2;
proc1.setStandardOutputProcess(&process2);
QString cmd1("grep \"false negatives\" test.txt");
QString cmd2("cut -f2");
proc1.start(cmd1);
proc2.start(cmd2);
I want execute a commande line with QProcess :
QString elf_path=C:\\files\\file.elf;
QString appli = "readelf.exe -a "+elf_path+" >>C:\\work\\essai.txt";
QProcess *process = new QProcess();
process->execute(appli);
but QT display this error :
readelf: Error: '>>C:\work\essai.txt': No such file
Can you help me ?
The QProcess::execute command will take the first parameter as the executable and pass each of the next parameters as arguments to that executable. So the error is because the readelf executable is receiving ">>C:\work\essai.txt" as an argument.
There is more than one solution to fix this.
Rather than redirecting the output to the text file, you could read the output from the readelf command (readAllStandardOutput), open a file essai.txt from Qt and append the output yourself. You should probably call waitForFinished() before retrieving the output.
Alternatively, there's a function in QProcess called setStandardOutputFile which takes a filename to redirect the output from the process to that file, which may be easier: -
QProcess* proc = new QProcess;
QString appli = "readelf.exe -a " + elf_path;
proc->setStandardOutputFile("C:\\work\\essai.txt", QIODevice::Append);
proc->start(appli);
Finally, you could create a shell script and call that with your parameters where the shell script would know that the final input parameter is to use for the output redirection.
QProcess::execute is static method. You should not create instance of QProcess in your case. Try next code
const QString path2exe = "readelf.exe";
QStringList commandline;
commandline << "-a";
commandline << elfPath;
commandline << "c:\\work\\essai.txt"
QProcess::execute( path2exe, commandline );
It looks like readelf is seeing your redirection as another file, which is valid since readelf can handle more than one on the command line.
Hence, the Qt process stuff is not handling redirection as you expect.
Within a shell of some sort, the redirections are used to set up standard input/output (and possibly others) then they're removed from the command line seen by the executable program. In other words, the executable normally doesn't see the redirection, it just outputs to standard output which the shell has connected to a file of some sort.
In order to fix this, you'll either have to run a cmd process which does understand redirection (passing the readelf command as a parameter) or use something like the method QProcess::readAllStandardOutput() to get the output into a byte array instead of writing to a temporary file.
I have a C++ program which I need to run it multiple times.
For example:-
Run ./addTwoNumbers 50 times.
What would be a good approach to solve this problem?
In POSIX shells,
for i in {1..50} ; do ./addTwoNumbers ; done
If this is code you are writing, take the number of times you want to "run" as an argument:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
int numTimes = 1;
if (argc > 1)
{
numtimes = atoi(argv[1]);
}
for (int i = 0; i < numTimes; i++)
{
// Your code goes here
}
}
(Note this doesn't do any sanity checking on the input, but it should point you in the right direction)
The way you were asking the question indicated that you had a finished binary. You want to run it as if it was from the command line. The forward slash, to me, is a clue that you are a Unix like operating system user. Well, that, and the fact that this post is tagged "Unix", which I just saw after writing the below. It should all be applicable.
The scheme of using the shell is probably the simplest one.
man bash tells you how to write a shell script. Actually we need to figure out what shell you are using. From the command line, type:
echo $SHELL
The response I get is
/bin/bash
Meaning that I am running bash. Whatever you get, copy down, you will need it later.
The absolutely lowest knowledge base is to simply create a file with any standard text editor and no suffix. Call it, simply (for example) run50.
The first line is a special line that tells the unix system to use bash to run the command:
#! /bin/bash
(or whatever you got from echo $SHELL).
Now, in the file, on the next line, type the complete path, from root, to the executable.
Type the command just as if you were typing it on the command line. You may put any arguments to your program there as well. Save your file.
Do you want to run the program, and wait for it to finish, then start the next copy? Or do you want to start it 50 times as fast as you can without waiting for it to finish? If the former, you are done, if the latter, end the line with &
That tells the shell to start the program and to go on.
Now duplicate that line 50 times. Copy and paste, it is there twice, select all, and then paste at the end, for 4 times, again for 8, again for 16, and again for 32. Now copy 18 more lines and paste those at the end and you are done. If you happen to copy the line that says #! /bin/bash don't worry about it, it is a comment to the shell.
Save the file.
From the command line, enter the following command:
chmod +x ./filenameofmyshellcommand
Where you will replace filenameofmyshellcommand with the name of the file you just created.
Finally run the command:
./filenameofmyshellcommand
And it should run the program 15 times.
If you are using bash, instead of duplicating the line 50 times, you can write a loop:
for ((i=1;i<=50;i++)) do
echo "Invocation $i"
/complete/path/to/your/command
done
I have included a message that tells you which run the command is on. If you are timing the program I would not recommend a "feelgood" message like this. You can end the line with & if you want the command to be started and the script to continue.
The double parenthesis are required for this syntax, and you have to pay your syntax.
for ((i=1;i<=50;i++)) do echo "invocation $i" & done
is an interesting thing to just enter from the command line, for fun. It will start the 50 echos disconnected from the command line, and they often come out in a different order than 1 to 50.
In Unix, there is a system() library call that will invoke a command more or less as if from the terminal. You can use that call from C++ or from perl or about a zillion other programs. But this is the simplest thing you can do, and you can time your program this way. It is the common approach in Unix for running one program or a sequence of programs, or for doing common tasks by running a series of system tools.
If youy are going to use Unix, you should know how to write a simple shell script.
int count=0;
int main()
{
beginning:
//do whatever you need to do;
int count++;
if (count<=50);
{
goto beginning;
}
return 0;
}