Redirecting Pipe Output in C++ - c++

I’m looking for a C/C++ way to execute a shell command and store the output of that command in a std::string or file, without the output being automatically printed to the console.
All approaches I’ve seen can do exactly that, but also print the execution result to the console.
For example with a combination of
FILE* pipe = popen("ls", "r");
and
fgets()
I’m able to do just that, but with the printing to the console.
Is there perhaps a way to redirect the stream’s buffer from std::cout to std::sstream, or has it semething to do with Ubuntu?
Any help is appreciated :)

The 2>&1 part from the comments did it.

Related

subprocess.popen stream handling

Is it possible to prevent subprocess.popen from showing prompts in the terminal?
Attempting to map a drive but would like to read the prompt for credentials in the script rather than display them to the terminal. The idea being I can carry out actions based on the response.
I am aware the use of shell is frowned upon when using string based commands (for obvious reasons), however I'm controlling the input so am happy with the risk for testing purposes.
I was under the impression that all stdout (interaction) would be parsed into the output_null variable. Instead I am still getting the prompt in the terminal (as illustrated below). I'm either miss understanding how the streams work or I'm missing something. Can anyone enlighten me please
command = "mount -t smbfs //{s}/SYSVOL {m}".format(s=server, m=temp_dir)
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output_null = p.communicate()[0]
if "Password for" in output_null:
print 'awdaa'
Terminal Shows
Password for 192.168.1.111:

New to C++, I do not understand how to display output in a .txt file

I am very new to C++. I have my code and it displays my desired output in a win32 console.
However, my Instructor wants the output to be run through a .txt file. We have done this before with a program that had input already written within the coding.
ex.
cout << "example1....example2";
We achieved this with his exact instructions:
*1) Probably the easiest way to obtain a hard copy of the generated
program output on a Microsoft Windows platform is to run your
program from a command prompt, redirecting the output to a file.
The command line syntax would like like this:
lab1prog >lab1.txt*
My problem, however, is that I did this again for lab2 and redirected the output to lab2.txt but I need user input for this time around. When I run the lab2.exe file, my lab2.txt file outputs my "cout" statement and waits for input but I cannot enter input through a .txt file.
Please help if you can.
When you redirect the output of the program with
lab1prog >lab1.txt
then the user is still able to input data. The only problem is that she doesn't know when to enter what data. This is usually done by prompt outputs that are hidden whith the >lab1.txt.
To circumvent the problem you could abuse the error output what is not redirected with the command above.
cerr << "prompt";
To avoid this abuse you should use a "tee" program like wintee instead of redirecting with a simple >lab1.txt.
If it's acceptable to get the input from a file instead from the user you can use the input redirection.
labprog1 <input.txt >lab1.txt
If you specifically want to use a text file as input into your program, the easiest way (as Kamil Mikolajczyk mentioned) is to run it this way:
> lab1prog >lab1.txt <input.txt
On the other hand, if you need to run the program as it is, but have complete control over what to output into the file, I'd suggest using a file handle. Check out this question on how to use file handles. You could even duplicate the output on the command prompt and the file when you want by outputting it as:
fout << "My output";
cout << "My output";
You can as well output the user input into your output file for your convenience.

Executing a .bat file with a parameter and reading the console output in C++

I have a batch file I need to execute, it has one parameter, If I were to run this script myself I would open up cmd and write
lexparser.bat texfile.txt
and the output would then be printed to the console.
I have shopped around and I have found some code which seems to be executing the file but I can't seem to extract the data being output, but I am unsure if this is correct.
QString pathDocument = qApp->applicationDirPath()+ "/stanford/lexparser.bat";
long result = (long)ShellExecute(0, 0, reinterpret_cast<const WCHAR*>(pathDocument.utf16()), 0, 0, SW_NORMAL);
I am using C++ as my language and I am also using the Qt Library to help me.
I have limited programming ability so any help would be greatly appreciated
I would recommend using QProcess for doing that.
See this question and its accepted answer for an example on how to do so.

Wmic /format switch invalid XSL?

I have a quick question, should be relatively simple for those who have some more experience in WMI-command processor than I do (and since I'm an absolute beginner thats not hard :-) )
I fail to understand why wmic /format switch works the way it does. I open up cmd.exe and type
wmic process list brief /format:htable > processlist.html
this does exactly what I want and no bothers further on. Whereas if I go to wmic processor, and try to execute the same command exactly as above...
wmic:root\cli>process list brief /format:htable > processlist.html
I receive the error tag: "Invalid XSL format (or) file name."
Here goes the screenshot. Note I have already copied XSL files from wbem to sys32 dir
Can someone explain to me why these 2 commands that for me look exactly the same, with the only difference that one is executed outside wmic environment and the other one is from inside, the latter one doesn't work? I just fail to understand it.
Please advise so I can comprehend this a bit better! :-)
Try this
copy /y %WINDIR%\system32\wbem\en-US\*.xsl %WINDIR%\system32\
And then
wmic:root\cli>process list brief /format:htable.xsl > processlist.html
Note the presence of the extension after "htable"
You are attempting to use CMD.EXE > redirection while you are within the interactive WMIC context. That can't work.
You can use the WMIC /output:filename switch while in interactive mode. Each subsequent command will overwrite the output of the previous command. You can get multiple commands to go to the same file by using /append:filename instead. You can reset the output back to stdout using /output:stdout.
/output:processlist.html
process list brief /format:htable
/output:stdout
Did you try specifying a full path in the wmic:root\cli>process call? My bets are that the first worked because it output the file to the current directory.

Linux console commands in C++ (gcc compiler)

How can I give commands to Linux console (Ubuntu) from my c++ program, and assign a value, which my command tells, to string variable? Please, give me an example, in which program gives simple command "uname -a" to console and writes result.
Sorry for my bad English, I know it very little. I would be very happy, if someone will write his answer in Russian (if it allowed) . I was looking for the answer to my question in Russian resources, but found nothing, you're my last hope.
The command you need is popen. You can get information about it by typing man popen into your shell; if your Linux distribution runs its Russian translation, it should display the information about it in Russian.
Basically, popen just opens a "file" (stream), with which you can work just like with a regular file. Here's an example of how it could be used:
#include <stdio.h>
int main()
{
FILE *f;
char stuff[100];
f = popen("uname -a", "r");
fgets(stuff, 100, f);
printf("%s", stuff);
pclose(f);
}
The code above doesn't have any error handling; you should insert the appropriate checks after you read and understand the complete manual page (rus).
Look for Russian language resources that explain the popen(3) library routine. You will need to use popen to launch the command, then read the pipe to obtain the output.