使用cin用C ++命名tarball

时间:2015-09-06 00:10:28

标签: c++ zip gzip tar

这是我当前难以接受的代码。是的我是C ++的新手

#include <iostream>
#include <string>

int main()
{
    using std::string;
    using std::cin;
    using std::cout;
    string projectname;

    std::cout << "Enter your project folders name: ";
    std::cin >> projectname;

    // Idea is to call tar, gzip and zip

    // Create the tarball from using cin for file title
    system("tar -cvf projectname.tar");  projectname;

    // using cin gzip the tarball
    system("gzip projectname.tar");

    // then call md5sum and sha1sum to get the hash for each
    system("md5sum projectname.tar.gz > gz.log");

    system("pause");
    return 0;
}

这不起作用,我需要它从cin

获取文件变量

提前致谢。

1 个答案:

答案 0 :(得分:0)

您需要在相关位置添加插入变量 projectname 的命令的单独部分,如下所示:

#include <iostream>
#include <string>

int main()
{
    using std::string;
    using std::cin;
    using std::cout;
    string projectname;

    std::cout << "Enter your project folders name: ";
    std::cin >> projectname;

    // Idea is to call tar, gzip and zip

    // add the different parts into a string
    // then convert to a char const* using c_str()
    system(("tar -cvf '" + projectname + ".tar' '" + projectname + "'").c_str());

    // Same thing broken down

    string command = "gzip '";
    command += projectname;
    command += ".tar'";

    system(command.c_str());

    // etc....
}