I'm trying to read the input for a c++ file y.cpp
from the file file.in
and save the result in file.out
but I can't figure how to do it.
for example, if the following code was in y.cpp
#include<iostream>
using namespace std;
int main(){
int x;
cin>>x;
cout<<x<<" was entered";
}
and the content of file.in
was
56
I want a shell command to write 56 was enterdd
in file.out
What I did so far is compiling y.cpp
and printing the result in file.out
.
g++ y.cpp -o y.out
./y.out>file.out
But in my code, reading is from the terminal and not from file.in
, and I want to read from file.in
.
I searched for a piping method to pipe file.in
content into the g++
command instead of reading from the terminal. But I didn't find anything about piping input file. all what I found was piping source code into the compiler and not piping input.
I searched also for all the options of g++ that have file
parameter but could not find aything to answer my question.
How can I do it?How to read c++ input from external file instead of reading it from terminal?