为了更快地输入,我读到你可以做file-redirection并包含cin已设置输入的文件.
理论上它应该像下面这样使用
App.exe inputfile outputfile
据我在C++ Primer一书中所理解,以下C++代码[1]应该cin从文本文件中读取输入,不需要像[2]这样的任何其他特殊指示
[2]
include <fstream>
ofstream myfile;
myfile.open ();
[1]以下C++代码......
#include <iostream>
int main(){
    int val;
    std::cin >> val; //this value should be read automatically for inputfile
    std::cout << val;
    return 0;
}
我错过了什么吗?
gaw*_*awi 17
要使用您的代码[1],您必须像这样调用您的程序:
App.exe < inputfile > outputfile
您还可以使用:
App.exe < inputfile >> outputfile
在这种情况下,输出不会在每次运行命令时重写,但输出将附加到现有文件.
有关在Windows中重定向输入和输出的更多信息,请参见此处.
请注意<,>和/ >>符号应逐字输入- 它们不仅仅用于本说明中的演示目的.所以,例如:
App.exe < file1 >> file2
除了原始重定向>/ >>和<
您可以重定向std::cin和std::cout太.
如下:
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); 
}