gpt4 book ai didi

c++ - 如何更改 C++ 系统函数中命令的某些部分

转载 作者:行者123 更新时间:2023-11-30 03:39:51 24 4
gpt4 key购买 nike

所以我有这段代码。很简单

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

所以我希望用户将这两个字符串更改为他们需要的任何文件路径,并将其放入命令而不是 inputoutput 文件。这可能吗?

最佳答案

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*。

请注意,我没有测试过这段代码,但我在这里提出的想法应该可行。

关于c++ - 如何更改 C++ 系统函数中命令的某些部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38515337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com