1

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?

1 Answer 1

2

This is really nothing to do with gcc (or g++): what you need is file redirection

./y.out < file.in

or (to redirect the program's output as well as its input)

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .