using ImageMagick with C++ system call - c++

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());
}
}

Related

How to print the actual name of the file to the console (C++)

I am using the terminal and C++. My program is opening a text file and doing various things with it. However, towards the very end I just want to print out the file name. How do I do that? I know that argc will look at a certain argument, but this still just looks at the number of the argument. I need the actual string name.
To print out the first argument (in your case the file.txt file name supplied as an argument) use the following code:
#include <iostream>
int main(int argc, char* argv[]){
if (argc > 1){
std::cout << argv[1];
}
else {
std::cout << "No arguments passed in.";
}
}
If you execute the program via: ./test file.txt the console output will be:
file.txt
Please note that argv[0] will print out the name and the (calling) path of your executable which in your case is ./test, not the name of your text file (argument).

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.

windows, c++: send file to exe (c++ solution) and read data from sent file

My goal is to send an arbitrary text file to a exe which is a build of a c++ project. In the c++ project I would like to read the file which was sent to the exe. Hence, I think I need to pass the path of the sent file to the application (exe).
My c++ code [is working!]:
#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
std::string readLineFromInput;
ifstream readFile("input.txt"); // This is explizit.
// What I need is a dependency of passed path.
getline(readFile, readLineFromInput);
ofstream newFile;
newFile.open("output.txt");
newFile << readLineFromInput << "\n";
newFile.close();
readFile.close();
}
My Windows configuration:
In the following path I created a shortcut to the exe (build of c++ project):
C:\Users{User}\AppData\Roaming\Microsoft\Windows\SendTo
Question:
I want to right click to an arbitrary text file and pass it (SendTo) to the exe. How can I pass the path of the sent file as an argument to the application such that the application can read the sent file?
When the path is passed as an argument the line of code should be like this, I guess:
ifstream readFile(argv[1]);
Much thanks!
David
Whether you use SendTo or OpenWith, the clicked filename will be passed to your executable as a command-line parameter. As such, the argv[] array will contain the filename (at argv[1], unless your SendTo shortcut specifies additional command-line parameters, in which case you would have to adjust the argv[] index accordingly).
I just did a test with SendTo and argv[1] works fine. Just make sure you check argc before attempting to open the filename, eg:
int _tmain(int argc, _TCHAR* argv[])
{
if (argc > 1)
{
std::string readLineFromInput;
std::ifstream readFile(argv[1]);
if (readFile)
std::getline(readFile, readLineFromInput);
std::ofstream newFile(_T("output.txt"));
if (newFile)
newFile << readLineFromInput << "\n";
}
}

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.

Read values using `backticks` instead of Pipe | 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.