Read values using `backticks` instead of Pipe | C++ - c++

on shell there is the possibility to give a output to another programm using a Pipe.
for example :::
ps axu | grep someprocess
Now i want to programme a C++ Programme that accepts those Pipes too.
I found a solution like.
using namespace std;
int main()
{
string mypipe;
if(cin);
{
cin >> mypipe;
cout << mypipe << endl;
}
return 0;
}
Now i want, that i am able to call my function with using backticks.
for example i am using a shell construct like this.
./myprog.bin `./otherprog.bin someparameter`
How can i read the output that otherprog.bin generates into my Programm using Parameters instead?

Add commandline parameters to your main function, like this:
int main( int argc, const char* argv[] )
argc is the argument count, argv the table that holds them.

Related

How to use parameters from commandline inside a main function in C++ using SET-Command

Sorry, I have never really worked with Commandline parameters. Maybe this is a stupid question:
I have a simple file test.cpp with a main function. I now want to start this program from the Commandline after having set the variable var with a value from the Commandline. (How) Can I do this?
test.cpp:
int main(int argc, char *argv[])
{
int var; // do I need to declare this variable here?
if (var == 123)
puts(" var=123 !!");
}
In the Commandline can I type something like that:
set /A var=123
test
And main would print "var=123 !!"
set defines an environment variable. That's a variable that exists in the environment of your application, i.e. outside your application. You can't use that to set variables inside your program.
You can however access the environment using std::getenv.
#include <iostream>
#include <cstdlib>
int main(void) {
const char* env_abc = std::getenv("ABC");
if (env_abc)
{
int abc = std::atoi(env_abc);
std::cout << abc;
}
}
If you mean running from command line something like "test 123",
then the argv[] argument contains your command line, when argv[0] is the name of the process, and argv[1] and so on are your command line arguments represented as strings
int main(int argc, char *argv[])
{
int var = atoi(argv[1]);
if (var == 123)
puts(" var=123 !!");
}

Providing a file path to an input-dependent program

First off, sorry if the title makes no sense. The nature of my question makes it very hard for me to phrase.
I am working on an assignment for my datastructures class and I am completely and totally brand new to c++ due to only having learned Java at my old school. The project is a weather logger that reads in data from a text file climatedata.txt. My teacher has provided us with a main function in the file (that we are NOT allowed to modify in any way) weatherlog.cpp which is below.
#include <iostream>
#include <fstream>
#include "datalogger.h"
using namespace std;
int main(int argc, char** argv) {
datalogger dl;
if (argc != 2) {
cout << "Usage: " << argv[0] << " <datafile>" << endl;
exit(0);
}
// Read the data
char* datafile = argv[1];
ifstream infile(datafile);
int timestamp;
double temperature;
double windspeed;
while (!infile.eof()) {
infile >> timestamp;
infile >> temperature;
infile >> windspeed;
if (!infile.eof()) {
dl.addData(timestamp, temperature, windspeed);
}
}
// Output the report
dl.printReport();
return(0);
}
Initially I was confused as to why the program would never fully execute until I figured out what argc is in the scope of a main function. It seems that he wants me to provide the text file name while compiling so that argc will be 2 instead of 1 (the value I saw when debugging) so that it can actually execute the rest of the program instead of immediately exiting.
My problem is I'm not sure how to provide the program with the text file location. I've looked all over the internet but since I'm not even sure at which stage to provide the file path I haven't had any success. Is that information supposed to be passed when compiling with g++? After successfully compiling when I'm trying to run the executable? What does the terminal command to do so look like?
So I understand that you need to provide a file name in argv (Comment below if I'm incorrect). argv is an array of arguments passed by the commandline, and argc is the amount of arguments passed (automatically set). To do that simply call the program in terminal like this: ./<progam> <file_name>
Example:
compile just as you would a hello world progam.
Call the program weatherlog climatedata.txt.
If your file has spaces in its name either remove them or do this enclose its name in quotes.
argc stores number of passed in parameters, while argv points to parameters.
if (argc != 2) means checking number of input parameters passed in via Console mode. The first parameter is always the program name. From the second parameter you can pass anything you want. char* datafile = argv[1]; means taking the second parameter as data filename.
In short, open Console mode (CMD on Windows, Terminal on Linux) and type something like: yourprogram C:\path\to\climatedata.txt.

using ImageMagick with C++ system call

In my C++ program, I can use ImageMagick commands through the system call. For example, to display an image named button_out.miff, I can put the following code in my program:
system("display button_out.miff -display :0");
Now I want to pass the image file name "button_out.miff" as a parameter to my c++ program. For example, if my compiled program is named test.exe, I wish when I run the command:
test.exe button_out.miff
my program can pass button_out.miff as a parameter to the ImageMagick display command. How could I do that?
I guess it’s more about string concatenation?
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
if (argc > 1)
{
string filename = CW2AEX<>(argv[1]);
stringstream ss;
ss << "display " << filename << "-display :0";
system(ss.str().c_str());
}
}

openCV load_object_detect function

I am trying to run a face detection code in openCV and I don't want to use the command prompt to run it but I don't know what to give as an input argument to load_object_detect.
here is the sample from the code:
CvHaarClassifierCascade* cascade = load_object_detector(argv[2]);
In order for the above command to execute the code should be run from the command prompt which I am trying to avoid for now...
All I know is that its input type is const char* cascade_path...
try argv[2] = whatever path you want to put;
just before calling the function
Something like this
#include<iostream>
using namespace std;
int main(int argc , char *argv[])
{
argv[2] = "SuvP";
std::cout<<"Hey "<<argv[2]<<endl;
return 0;
}
The output is Hey SuvP
The arguments from command line are stored in the array argv. Alternatively we are filling data in the array within the code and not from the command line in this case.

How can I detect that input is being redirected in from a file?

I've written a program that takes its first argument and reverses the letters. So, for instance:
revstr cat
Will produce tac.
Now I want this to work when a file is redirected in. So, if filler.txt is a file containing "Now is the time for all good men to come to the aid of their country!", then:
revstr < filler.txt
Should produce:
!yrtnuoc rieht fo dia eht ot emoc ot nem doog lla rof emit eht si woN
But I don't know how to detect that such redirection is occurring!
This is what I've tried - obviously, it's no good. Where am I going wrong?
int main(int argc, char* argv[]) {
string temp,input,output;//store input from file, and get which file//
ofstream out("output.txt");
if(argc == 3)
{
if(ifstream(argv[2]))
{
input = argv[2];
ifstream in(input);
while(in.good())
{
in >> temp;
ReverseWord(temp);
cout << temp << endl;
out << temp << endl;
}
}
else
ReverseWord(argv[2]);
}
else
}
I'm fairly new to C++ and am doing my best to learn.
There are two possible approaches for you (well, you can even support both):
You can accept a file name as command line argument (using a main that accepts arguments), then open an ifstream using this filename as the stream to read from. Users use your program like revstr filename.txt.
You can read your input from std::cin. Then users need to use redirection to pass you the contents of a file. If your program is started using: revstr < filename.txt, then reading from std::cin will read the contents of the file. The program never even sees the filename.
You can support both by reading from an ifstream, if you get an argument, and from cin, if you don't get an argument. The function that does the reading can get the steam passed in as a generic istream&.
You should change your definition of your main() function so that it accepts arguments passed from command line:
int main(int argc, char* argv[])
The first variable will hold the number of command-line arguments provided, the second is a vector whose elements are pointers to NULL-terminated strings. These strings are the command-line arguments themselves. Please keep in mind, that the first string will always be the name of the executable.
For instance, supposing the name of the file to be opened will be passed as the first argument:
int main(int argc, char* argv[])
{
std::string filename;
if (argc > 1)
{
// Oh, there's some command line arguments here...
filename = argv[0];
}
// Go on processing...
}