Tactical Shrimp Tactical Shrimp - 4 months ago 9
Bash Question

How can I change certain parts of a command in the system function in C++

So I have this code. It's very simple

#include <iostream>
using namespace std;

int main() {

string inputfile = "input.pdf";
string outputfile = "output.tiff";
cout << "paste path of pdf to convert" << endl;
cin >> inputfile;
cout << "type the path of the output file with the correct extension ie png jpeg or tif" << endl;
cin >> outputfile;

system("gm.exe montage -tile 1x10000 -geometry 100% -density 200x200 input.pdf -quality 100 output.tif");

return 0;
}


So I want the user to change the two strings to whatever file path they need and put that into the command instead of
input
or
output
file. Is this even possible?

Answer
string inputfile = "input.pdf";
string outputfile = "output.tiff";
cout << "paste path of pdf to convert" << endl;
getline(cin, inputfile);
cout << "type the path of the output file with the correct extension ie png jpeg or tif" << endl;
getline(cin, outputfile);
string command = "gm.exe montage -tile 1x10000 -geometry 100% -density 200x200 " + inputfile + " -quality 100 " + outputfile;

system(command.c_str());

Note that I now use getline() to gather input from the user -- I've just heard this is a better method, so I changed it here. Then I created a new string variable and concatenated the inputfile and outputfile strings to it to create the command string. Since the system() function requires a const char* as its parameter, I then use c_str() to convert the string to a const char*.

Please not that I have not tested this code, but the idea I've presented here should work.

Comments