C++文件重定向

C++ file-redirection

本文关键字:重定向 文件 C++      更新时间:2023-10-16

为了更快的输入,我读到您可以执行file-redirection并包含一个已经设置了cin输入的文件。

理论上,它应该如下使用:

App.exe inputfile outputfile

据我从C++初级读本中了解,以下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;
}

我是不是错过了什么?

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

App.exe < inputfile > outputfile

您也可以使用:

App.exe < inputfile >> outputfile

在这种情况下,输出不会随命令的每次运行而重写,但输出将附加到现有文件中。

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


请注意,<>>>符号要逐字输入—在本解释中,它们不仅仅是为了演示目的。例如:

App.exe < file1 >> file2

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

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

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

您可以提供文件名作为可执行程序的命令行输入,但随后需要在代码中打开它们。

您已经提供了两个命令行参数,即inputfile&输出文件

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

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

常见的重定向命令有:

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

    $ 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:搜索文本模式,对其进行修改并输出