【问题标题】:use cin to name a tarball from with C++使用 cin 从 C++ 中命名一个 tarball
【发布时间】:2015-09-06 00:10:27
【问题描述】:

这是我目前的代码,我很难过。是的,我对 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 获取文件变量

提前致谢。

【问题讨论】:

  • 您可以使用stringstream“打印”完整的字符串,也可以使用+ 连接各个部分。如果你在你的 tar 命令中使用cvfz,你会直接从tar 得到一个.gz 文件。

标签: c++ zip gzip tar


【解决方案1】:

您需要添加将变量 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....
}

【讨论】:

  • Tar 仍然没有从 cin 中找到名称,文件夹存在
  • @Keldo 我不知道tar 是如何工作的,我只是来找C++ 的。但我认为您可能需要告诉 tar 要将哪些文件添加到存档中。从命令行找出正确的tar 命令,然后根据我的解释创建它,它应该可以工作。
  • tar -czvf projectname.tar.gz projectname 其中 projectname 来自 cin 但它仍然没有读取输入。 Tar 失败,因为它不会创建一个空目录,因为没有输入到达 projectname 变量
  • @Keldo 我根据阅读targzip 说明修改了我认为应该可行的答案。
  • “找不到文件夹”是什么意思?您的程序是否在正确的目录中执行? [为什么你首先在 C++ 中这样做,肯定有一些合适的脚本语言可以做到这一点而无需编译和填充]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-17
  • 1970-01-01
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 2011-01-24
相关资源
最近更新 更多