【问题标题】:How can we use .txt file as command line input in c++ language on a windows machine?我们如何在 Windows 机器上使用 .txt 文件作为 C++ 语言的命令行输入?
【发布时间】:2018-06-14 04:46:59
【问题描述】:

我想让我的 c++ 文件从一个文本文件中读取输入并将输出保存在另一个文本文件中。这是为了检查我的竞争性编程问题。

【问题讨论】:

  • 您的问题和描述说明了两种不同的情况。问题要求读取一些要执行的文本,描述要求读取一些文本作为标准输入。

标签: windows powershell c++11 command-line c++14


【解决方案1】:

我认为这个解决方案适合你。

#include <iostream>
#include <fstream>
//#define cin fin
//#define cout fout
using namespace std;
int main(){
    //ofstream fout("output.txt");
    //ifstream fin("input.txt");
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    for(int i=0;i<n;i++){
        cout<<arr[i]<<endl;
    }
    return 0;
}

如果您想用作标准输入输出,那么您可以按原样使用。根据您的问题,您只需删除两个斜杠(cmets)即可使用它。 在上面的程序中,“input.txt”是输入的文件名,“output.txt”是存储输出的文件。

注意:您只需提供“input.txt”,程序会自动生成“output.txt”。

您可以根据需要重命名“input.txt”和“output.txt”。

如果您对上述解决方案不满意,可以使用名为hightail 的程序。 Hightail 是一款用于编程竞赛的自动测试仪。了解更多关于 hightail 及其工作的信息click here。

【讨论】:

  • 是的,这种方式更适合原题中提到的竞技编程。
【解决方案2】:

不清楚你在问什么。因为你的问题和你的描述似乎在问两种不同的情况。

对于您的问题,您似乎在问如何从文件中输入命令(如dir 或systeminfo)。您可以使用头文件stdlib.h中的函数system()来执行从该文件中读取的命令。

对于您的描述,您似乎在问如何将文件中的一些数据输入到stdin并输出到另一个文件,您可以查找command redirection,例如Redirect Windows cmd stdout and stderr to a single file。

【讨论】:

  • 描述之一就是我要问的。您共享的答案不适用于 Windows。请帮忙。
  • @samarthsinghal “不起作用”是什么意思?有什么现象吗? executable &lt; input.txt &gt; output.txt 不行吗?
  • 由于你的标签有'powershell',你可以参考这个stackoverflow.com/questions/6763086/…
猜你喜欢
  • 1970-01-01
  • 2012-09-28
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 2016-07-16
  • 1970-01-01
  • 2021-07-19
相关资源
最近更新 更多