C++ 文件重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18081565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:42:26  来源:igfitidea点击:

C++ file-redirection

c++file

提问by zurfyx

For faster input, I read that you can do file-redirectionand include a file with the cininputs already set.

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

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 cininput from the text file and shouldn't need to any other special indication like[2]

据我从 C++ Primer book 中了解到,以下 C++ 代码 [1] 应该cin从文本文件中读取输入,并且不需要任何其他特殊指示,例如 [2]

[2]

[2]

include <fstream>
ofstream myfile;
myfile.open ();

[1] The following C++ code...

[1] 下面的 C++ 代码...

#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?

我错过了什么吗?

回答by gawi

To use your code [1] you have to call your program like this:

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

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.

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



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

回答by P0W

In addition to original redirection >/ >>and <

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

You can redirect std::cinand std::couttoo.

您可以重定向std::cinstd::cout太。

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

}

回答by Dr. Xperience

[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

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

[ App.exe 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;
}

回答by lmiguelvargasf

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>&1redirects stderr to the same file that stdout was redirected to.

    $ command > file 2>&1

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

    $ 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

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.
  • sort: 按字母顺序对文本行进行排序。
  • uniq: 过滤重复的相邻文本行。
  • grep: 搜索文本模式并输出它。
  • sed?:搜索一个文本模式,修改它,并输出它。