【问题标题】:how to read gcc input from external file如何从外部文件读取 gcc 输入
【发布时间】:2015-05-21 19:36:30
【问题描述】:

我正在尝试从文件 file.in 中读取 c++ 文件 y.cpp 的输入并将结果保存在 file.out 中,但我不知道该怎么做。

例如,如果下面的代码在y.cpp

#include<iostream>
using namespace std;
int main(){
int x;
cin>>x;
cout<<x<<" was entered";
}

file.in的内容是

56

我想要一个 shell 命令在 file.out 中写入 56 was enterdd

到目前为止,我所做的是编译 y.cpp 并将结果打印到 file.out

g++ y.cpp -o y.out
./y.out>file.out

但在我的代码中,读取来自终端而不是来自file.in,我想从file.in 读取。

我搜索了一种管道方法,将file.in 内容通过管道传输到g++ 命令中,而不是从终端读取。但我没有找到任何关于管道输入文件的信息。我发现的只是将源代码管道输入编译器,而不是管道输入。

我还搜索了所有具有 file 参数的 g++ 选项,但找不到任何东西来回答我的问题。

我该怎么做?如何从外部文件读取 c++ 输入而不是从终端读取?

【问题讨论】:

    标签: ubuntu gcc g++


    【解决方案1】:

    试试这个

    ./y.out < file.in > file.out
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      • 2011-07-18
      • 2012-12-25
      相关资源
      最近更新 更多