how to put input and output file in qtcreator - qt

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 << " ";
}

Related

Shebang line C. How does it work?

I was reading the Advanced Programming in UNIX and stumbled upon this example. What is the shebang line doing here? Here's the top part of the code.
#!/usr/bin/awk -f
BEGIN {
printf("#include \"apue.h\"\n")
printf("#include <errno.h>\n")
printf("\n")
printf("static void pr_sysconf(char *, int);\n")
printf("static void pr_pathconf(char *, char *, int);\n")
printf("\n")
printf("int\n")
printf("main(int argc, char *argv[])\n")
printf("{\n")
printf("\tif (argc != 2)\n")
printf("\t\terr_quit(\"usage: a.out <dirname>\");\n\n")
FS="\t+"
while (getline <"sysopt.sym" > 0) {
printf("#ifdef %s\n", $1)
printf("\tprintf(\"%s is defined (val is %%ld)\\n\", (long)%s+0);\n", $1, $1)
printf("#else\n")
printf("\tprintf(\"%s is undefined\\n\");\n", $1)
printf("#endif\n")
printf("#ifdef %s\n", $2)
The shebang line is (on Linux and most Unixes) understood by execve(2). However, POSIX don't specify anything about it. Your script should be run by e.g. GNU awk, assuming that script is an executable file (and you probably want it to be accessible from your PATH variable).
So when something (probably your Unix shell, but could be something else) is executing with execve that script, the /usr/bin/awk program gets executed. You are betting that this awk program is some implementation of AWK
The OS's routines for executing a file look for the two characters #! at the start of a file, and if present, instead of directly loading the file as a binary executable, they call the executable file referenced by the rest of that line, along with any command line argument therein, then the original file as a final argument.
That's quite an involved description; a couple of examples make it clearer:
If myFile contains:
#!/bin/echo My path is
... and we make it executable:
$ chmod +x myFile
... then when we run it, we get:
$ ./myFile
My path is /home/slim/myFile
Or if we change its contents to:
#!/bin/cat
Hello
world
Then when we run it, it prints itself ...
$ ./myFile
#!/bin/cat
Hello
world
This is generally useful when command it invokes is an interpreter which can work with the contents, and itself ignores the shebang. Of course in many languages # denotes a comment, so we get this for free:
#!/bin/bash
#!/usr/bin/perl
#!/bin/awk -f
So essentially it arranges matters such that running myFile directly, is equivalent to running awk -f myFile.

How can I run an executable program and send it input in one line on command line?

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

Qprocess and command MSDOS

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.

QProcess start with files from stdin and stdout

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");

QProcess::setStandardOutputFile only creates 0kb File

I'm using a simple QProcess-Project on a WindowsXP-Machine:
QString program = "U:\\ffmpeg.exe";
QStringList arguments;
arguments << "-i" << "U:\\clock.avi" << "U:\\tmp_jpeg\\foo-%03d.jpeg";
process.setStandardOutputFile("U:\\log.txt", QIODevice::Append);
process.start(program, arguments);
The Process works just fine, ffmpeg creates all the files i want to. But the log-File keeps completely empty. The same happens when I want to write the standard-output at qDebug()...
Why does this happen and how can I fix it?
This happens because usually processes print into two files: "standard output" file and "standard error" file. Programmer can manually decide which file to output to (they're accessed via std::cout and std::cerr). The rule of thumb is to print to stdout the actual result of the program, and to stderr - errors, diagnostics etc.
I run ffmpeg and it so happens, that it prints nothing to stdout (probably, reserving it for special mode, where encoded file is printed there), and all text messages are printed to stderr. So you should use setStandardErrorFile() function to capture the output.

Resources