16

为了更快地输入,我读到您可以执行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;
}

我错过了什么吗?

4

4 回答 4

22

要使用您的代码 [1],您必须像这样调用您的程序:

App.exe < inputfile > outputfile

您还可以使用:

App.exe < inputfile >> outputfile

在这种情况下,不会在每次运行命令时重写输出,但输出将附加到已经存在的文件中。

有关在 Windows 中重定向输入和输出的更多信息,您可以在此处找到。


请注意,要逐字<输入,>和符号— 在本说明中,它们不仅用于演示目的。因此,例如:>>

App.exe < file1 >> file2
于 2013-08-06T13:28:25.833 回答
5

除了原始重定向>/>><

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); 

}
于 2013-08-06T13:54:04.783 回答
0

[我只是在解释问题中使用的命令行参数]

您可以将文件名作为命令行输入提供给可执行文件,但是您需要在代码中打开它们。

喜欢

您提供了两个命令行参数,即 inputfile 和 outputfile

[ App.exe inputfile outputfile]

现在在你的代码中

#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;
}
于 2013-08-06T14:13:21.133 回答
0

了解重定向的概念很重要。重定向重新路由标准输入、标准输出和标准错误。

常见的重定向命令有:

  • > 将命令的标准输出重定向到文件,覆盖以前的内容。

    $ command > file

  • >> 将命令的标准输出重定向到文件,将新内容附加到旧内容。

    $ command >> file

  • < 将标准输入重定向到命令。

    $ command < file

  • | 将命令的标准输出重定向到另一个命令。

    $ command | another_command

  • 2>将标准错误重定向到文件。

    $ command 2> file

    $ command > out_file 2> error_file

  • 2>&1将 stderr 重定向到 stdout 重定向到的同一文件。

    $ command > file 2>&1

您可以结合重定向:

# 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

即使这不是您的问题的一部分,您也可以在与重定向命令结合使用时使用其他强大的命令:

  • sort:按字母顺序对文本行进行排序。
  • uniq:过滤重复的、相邻的文本行。
  • grep: 搜索文本模式并输出。
  • sed : 搜索文本模式,修改它,然后输出它。
于 2018-07-27T21:06:09.303 回答