Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ file-redirection

Tags:

c++

file

For faster input, I read that you can do file-redirection and include a file with the cin inputs already set.

In theory it should be used like following:

App.exe inputfile outputfile

As far as I understood from C++ Primer book, The following C++ code[1] should be reading cin input from the text file and shouldn't need to any other special indication like[2]

[2]

include <fstream>
ofstream myfile;
myfile.open ();

[1] The following C++ code...

#include <iostream>
int main()
{
    int val;
    std::cin >> val; //this value should be read automatically for inputfile
    std::cout << val;
    return 0;
}

Am I missing something?

like image 877
zurfyx Avatar asked Aug 06 '13 13:08

zurfyx


People also ask

What is file redirection in C?

Before the C shell executes a command, it scans the command line for redirection characters. These special notations direct the shell to redirect input and output. You can redirect the standard input and output of a command with the following syntax statements: Item.

What is redirection operator in C?

A redirection operator is a special character that can be used with a command, like a Command Prompt command or DOS command, to either redirect the input to the command or the output from the command.

What is IO redirection?

Input/Output (I/O) redirection in Linux refers to the ability of the Linux operating system that allows us to change the standard input ( stdin ) and standard output ( stdout ) when executing a command on the terminal. By default, the standard input device is your keyboard and the standard output device is your screen.

How can we copy one file into another file using redirection in C++?

I/O Redirection in C++FILE * freopen ( const char * filename, const char * mode, FILE * stream ); For Example, to redirect the stdout to say a textfile, we could write : freopen ("text_file. txt", "w", stdout);


1 Answers

To use your code [1] you have to call your program like this:

App.exe < inputfile > outputfile

You can also use:

App.exe < inputfile >> outputfile

In this case the output wouldn't be rewritten with every run of the command, but output will be appended to already existing file.

More information about redirecting input and output in Windows you can find here.


Note that the <, > and >> symbols are to be entered verbatim — they are not just for presentation purposes in this explanation. So, for example:

App.exe < file1 >> file2
like image 186
gawi Avatar answered Oct 23 '22 23:10

gawi