16

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?

zurfyx
  • 31,043
  • 20
  • 111
  • 145

4 Answers4

22

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
gawi
  • 2,843
  • 4
  • 29
  • 44
  • 5
    You can also use `App.exe < inputfile >> outputfile`. Notice the extra '>'. This way the new output will be appended to the file instead of erasing it's previous contents. – Kelm Aug 06 '13 at 13:29
  • 1
    Thanks @gawi, I thought that the < and > were used to indicate a variable or something. – zurfyx Aug 06 '13 at 13:31
  • @Jerry You're welcome, btw if you want to speed up your program more you can use printf/scanf functions, or if you really want to speed up reading/writing data use fgets/puts functions (but to do this you must know what you are doing :)) – gawi Aug 06 '13 at 13:41
  • @gawi yeah, now it's still saying bullshit about `printf/scanf` being faster than `cin/cout`, but at least it's gramatically correct. – Bartek Banachewicz Aug 06 '13 at 13:47
  • @BartekBanachewicz So in your opinion cin/cout are comparable in execution time to scanf/printf? Maybe they improved it recently, but in the past, even when you disabled synchrinzation: std::ios_base::sync_with_stdio(false); streams were far more slower than printf/scanf functions. I haven't been using c++ for the last few years... – gawi Aug 06 '13 at 13:53
  • [This answer](http://stackoverflow.com/a/2873251/752976) has some interesting data. – Bartek Banachewicz Aug 06 '13 at 13:58
  • Interesting. They had to make a lot of changes in the past few years (either improving streams or introducing bugs in printf/scanf functions :)), because I remember making similar comparison around 8 years ago and there was a huge differemce. I would like to see similar comparison between scanf/cin. Anyway thanks for pointing this out. – gawi Aug 06 '13 at 14:28
5

In addition to original redirection >/ >> and <

You can redirect std::cin and std::cout too.

Like following:

int main()
{
    // Save original std::cin, std::cout
    std::streambuf *coutbuf = std::cout.rdbuf();
    std::streambuf *cinbuf = std::cin.rdbuf(); 

    std::ofstream out("outfile.txt");
    std::ifstream in("infile.txt");

    //Read from infile.txt using std::cin
    std::cin.rdbuf(in.rdbuf());

    //Write to outfile.txt through std::cout 
    std::cout.rdbuf(out.rdbuf());   

    std::string test;
    std::cin >> test;           //from infile.txt
    std::cout << test << "  "; //to outfile.txt

    //Restore back.
    std::cin.rdbuf(cinbuf);   
    std::cout.rdbuf(coutbuf); 

}
P0W
  • 46,614
  • 9
  • 72
  • 119
0

[I am just explaining the command line argument used in Question]

You can provide file name as command line input to the executible, but then you need to open them in your code.

Like

You have supplied two command line arguments namely inputfile & outputfile

[ App.exe inputfile outputfile ]

Now in your code

#include<iostream>
#include<fstream>
#include<string>

int main(int argc, char * argv[])
{
   //argv[0] := A.exe
   //argv[1] := inputFile
   //argv[2] := outputFile

   std::ifstream vInFile(argv[1],std::ios::in); 
   // notice I have given first command line argument as file name

   std::ofstream vOutFile(argv[2],std::ios::out | std::ios::app);
   // notice I have given second command line argument as file name

   if (vInFile.is_open())
   {
     std::string line;

     getline (vInFile,line); //Fixing it as per the comment made by MSalters

     while ( vInFile.good() )
     {
         vOutFile << line << std::endl;
         getline (vInFile,line);          
     }
     vInFile.close();
     vOutFile.close();
  }

  else std::cout << "Unable to open file";

  return 0;
}
Dr. Xperience
  • 475
  • 1
  • 5
  • 18
  • The code adds a spurious last line. When the last read fails, you _first_ write `line` to `vOutFile` and only then check `vInFile.good()`. The correct order in the loop should be : `read line, check if read succeeded, write line, goto read` – MSalters Aug 06 '13 at 14:46
0

It is important that you understand the concept of redirection. Redirection reroutes standard input, standard output, and standard error.

The common redirection commands are:

  • > redirects standard output of a command to a file, overwriting previous content.

    $ command > file

  • >> redirects standard output of a command to a file, appending new content to old content.

    $ command >> file

  • < redirects standard input to a command.

    $ command < file

  • | redirects standard output of a command to another command.

    $ command | another_command

  • 2> redirects standard error to a file.

    $ command 2> file

    $ command > out_file 2> error_file

  • 2>&1 redirects stderr to the same file that stdout was redirected to.

    $ command > file 2>&1

You can combine redirection:

# redirect input, output and error redirection
$ command < in_file > out_file  2> error_file
# redirect input and output
$ command < in_file > out_file
# redirect input and error
$ command < in_file 2> error_file
# redirect output and error
$ command > out_file  2> error_file

Even though, it is not part of your question, you can also use other commands are powerful when combined with redirection commands:

  • sort: sorts lines of text alphabetically.
  • uniq: filters duplicate, adjacent lines of text.
  • grep: searches for a text pattern and outputs it.
  • sed : searches for a text pattern, modifies it, and outputs it.
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228