【问题标题】:Auto input in VS code terminal from input.txt whenever compiled编译时从 input.txt 在 VS 代码终端中自动输入
【发布时间】:2021-10-08 13:24:43
【问题描述】:

通常我们需要在运行任何包含 std::cin 的文件后输入输入,如下面的 c++ 代码

    int M,N;
    cin>>M>>N;
    int i,a[M],b[N];
    for(i=0;i<M;i++)
    {
        cin>>a[i];
    }
    for(i=0;i<N;i++)
    {
        cin>>b[i];
    }
    Solution ob;
    cout<<ob.countPairs(a, b, M, N)<<endl;

我只是不喜欢每次都输入相同的大输入。所以我想为相同的输入自动化这个过程,比如我将输入保存在一个名为 input.txt 的文件中,运行该文件后,它应该从 input.txt 获取输入并输出结果。 Ofc 将输入保存到剪贴板是一种方法,但我可能想复制其他内容,而编码和复制粘贴本身又是一项小工作。

我在 ubuntu 中使用 VS 代码编辑器,并使用 coderunner 扩展在终端中运行代码。

【问题讨论】:

    标签: c++ visual-studio-code input auto vscode-code-runner


    【解决方案1】:

    脚本

    将您的长输入写入文件input.txt

    the quick brown fox jumped over the lazy dog
    

    使用 bash 脚本,script.sh:

    # Compile your program, ie:
    clang++ source.cpp -o application
    
    # Check compilation succeeded
    if [[ $? -ne 0 ]]; then
        echo "compilation failed"
        exit 1
    fi
    
    # Pipe your input into the application
    cat input.txt | ./application 
    

    最后,调用你的脚本:

    $ bash script.sh

    阅读:

    【讨论】:

    • ./application &lt; input.txt 有什么问题
    • 没什么问题,结果一样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多