2016-07-21 10 views
1

このコードはあります。これは、だから私は、ユーザーが、彼らが必要とするものは何でも、ファイルパスに2つの文字列を変更し、代わりにinputまたはoutputファイルのコマンドにそれを置きたいC++のシステム関数でコマンドの特定の部分を変更するには

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

非常に簡単です。これも可能ですか?

+1

@Matt Coatsの回答はあなたの状況に非常に良いですが、次回は従来のコマンドライン引数処理の使用を検討します。プラットフォームやオーディエンスによっては、これはもっと標準的かもしれません。 –

答えて

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

私は今、ユーザからの入力を収集するためにgetline()を使用することに注意してください - 私はこれがより良い方法である聞いたので、私はここでそれを変更しました。次に、新しい文字列変数を作成し、inputfileoutputfileの文字列を連結してコマンド文字列を作成しました。 system()関数はconst char *をパラメータとして必要とするため、c_str()を使用して文字列をconst char *に変換します。

私はこのコードをテストしていませんが、私がここに示した考え方はうまくいくはずです。

+0

'getline'が優れている理由の1つは、スペースがあるために、* c:¥documentとsettings¥batman¥desktop¥batmobile v4.2.7 design schematics.pdf *を解析できません。 – user4581301

+0

コマンドの2つのファイル名を二重引用符で囲まないと、GraphicsMagickはファイル名にスペースを含むファイルを処理しません。 –

+0

皆さん、ありがとうございました。私が今必要だったのは、 –

関連する問題