【问题标题】:C++ file-redirectionC++ 文件重定向
【发布时间】:2013-08-07 12:59:49
【问题描述】:

为了更快的输入,我读到您可以使用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;
}

我错过了什么吗?

【问题讨论】:

  • 是的——不应该这样使用。

标签: c++ file


【解决方案1】:

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

App.exe < inputfile > outputfile

你也可以使用:

App.exe < inputfile >> outputfile

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

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


请注意,&lt;&gt;&gt;&gt; 符号将被输入逐字——在本说明中,它们不仅仅用于演示目的。所以,例如:

App.exe < file1 >> file2

【讨论】:

  • 您也可以使用App.exe &lt; inputfile &gt;&gt; outputfile。注意额外的“>”。这样,新的输出将被附加到文件中,而不是删除它以前的内容。
  • 谢谢@gawi,我以为是用来表示变量什么的。
  • @Jerry 不客气,顺便说一句,如果您想加快程序速度,可以使用 printf/scanf 函数,或者如果您真的想加快读取/写入数据的速度,请使用 fgets/puts 函数(但要做到这一点,你必须知道你在做什么:))
  • @gawi 是的,现在它仍然在说 printf/scanfcin/cout 快,但至少在语法上是正确的。
  • @BartekBanachewicz 所以在您看来 cin/cout 在执行时间上与 scanf/printf 相当吗?也许他们最近改进了它,但在过去,即使你禁用了同步: std::ios_base::sync_with_stdio(false);流比 printf/scanf 函数慢得多。在过去的几年里我没有使用过 c++...
【解决方案2】:

除了原来的重定向&gt;/&gt;&gt;&lt;

您也可以重定向std::cinstd::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); 

}

【讨论】:

    【解决方案3】:

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

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

    喜欢

    您提供了两个命令行参数,即输入文件和输出文件

    [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;
    }
    

    【讨论】:

    • 代码添加了虚假的最后一行。当最后一次读取失败时,您首先line 写入vOutFile,然后才检查vInFile.good()。循环中的正确顺序应该是:read line, check if read succeeded, write line, goto read
    【解决方案4】:

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

    常见的重定向命令有:

    • &gt; 将命令的标准输出重定向到文件,覆盖之前的内容。

      $ command &gt; file

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

      $ command &gt;&gt; file

    • &lt; 将标准输入重定向到命令。

      $ command &lt; file

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

      $ command | another_command

    • 2&gt; 将标准错误重定向到文件。

      $ command 2&gt; file

      $ command &gt; out_file 2&gt; error_file

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

      $ command &gt; file 2&gt;&amp;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:搜索文本模式,对其进行修改并输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-20
      相关资源
      最近更新 更多