Command to run C++ with input from file
c++, command
Solution
You need to pipe the input file to your program when invoking it from the command line. Consider the following program:
#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;
}
... say I compiled it as "test.exe", I would invoke it as follows to pipe the input text file.
./test.exe < input.txt
Problem
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; } ``` The file `input.txt` may contains: Input.txt 10 20 Command lines to build and run the code- ``` g++ myC.cpp -o myC myC ``` It produces output `30` getting input from `input.txt` file. Now I am looking for a command which will similarly get input from a file, but want to avoid using freopen() inside the code. It might be something like this- ``` g++ myC.cpp -o myC // To compile myC -i input.txt // To run with input ```