Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。












想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。

7年前关闭。



Improve this question




myC.cpp
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
    freopen("input.txt","r",stdin); // All inputs from 'input.txt' file

    int n,m;
    cin>>n>>m;
    cout<<(n+m)<<endl;
    return 0;
}

文件input.txt可能包含:

Input.txt



用于构建和运行代码的命令行-
g++ myC.cpp -o myC
myC

它产生输出30,从input.txt文件获取输入。

现在,我正在寻找一个命令,该命令将类似地从文件中获取输入,但是要避免在代码内使用freopen()。

可能是这样的-
g++ myC.cpp -o myC  // To compile
myC -i input.txt    // To run with input

最佳答案

从命令行调用输入文件时,需要将输入文件通过管道传递给程序。考虑以下程序:

#include <stdio.h>

int main( void ) {

  int a, b;

  scanf( "%d", &a );
  scanf( "%d", &b );

  printf( "%d + %d = %d", a, b, ( a + b ) );

  return 0;
}

...说我将其编译为“test.exe”,我将按以下方式调用它以管道输入文本文件。
./test.exe < input.txt

10-07 19:09
查看更多