Clubbing a .txt file with a .cpp file

I have a file (.txt) and I have a C++ program based on trie. Now what I need is the search function performs search on data taken form that .txt file. What I should do now to achieve that??

Use freopen function: http://www.cplusplus.com/reference/cstdio/freopen/

freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);

To take input from input.txt and output to output.txt

You can also use a pipe if running from command line without adding anything to your normal C/C++ program. The command is as follows :- (assuming a.out is your program executable)

./a.out < input.txt > output.txt

A ‘>’ or ‘<’ defines the direction of data flow from left to right or vice versa respectively.

Hence, if you wish to output to terminal and only take input from input.txt, your command will be :-

./a.out < input.txt

1 Like