C++文件重定向
C++ file-redirection
为了更快的输入,我读到您可以执行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::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);
}
[我只是在解释问题中使用的命令行参数]
您可以提供文件名作为可执行程序的命令行输入,但随后需要在代码中打开它们。
像
您已经提供了两个命令行参数,即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
:搜索文本模式,对其进行修改并输出
- 获取通过重定向传入的输入文件的名称?
- 写入文件 a.txt,而终端重定向到文件 b.txt
- 将 clangd stderr 重定向到文件而不是控制台
- 是否可以将子进程的 stdout 重定向到父进程中的另一个文件?
- 重定向输入和输出时如何处理主文件I/O?
- 使用重定向命令从 stdin 读入的字符串"<"输入文件未正确附加
- 可以将Boost消息队列文件重定向到用户指定的位置
- 如何使用PHP将上传到本地主机的一个html文件重定向到磁盘上的另一个html文档
- 尝试将 std::cerr 重定向到文件时出现访问冲突异常
- 重定向到文件的C ++ COUT比使用Ofstream慢
- 将 Tcl 中的命令输出重定向到文件时,文件以 DOS 格式生成
- 将 c++ 程序语法错误重定向到窗口中的文件
- 如何在没有任何缓冲区的情况下将 stderr 重定向到文件?
- 将 ffmpeg 控制台输出重定向到C++中的字符串或文件
- Win32 GUI C(++) 应用将标准输出和标准输出重定向到磁盘上的同一文件
- 即使可执行文件的输入是从文件重定向的,如何强制从终端输入?
- C++输入重定向导致文件参数工作时出错
- C++ vector<pair<int,int>> std::all_of() 函数问题(重定向到头文件)
- 重定向文件访问
- Boost.IOStreams:如何使用"rdbuf"正确重定向文件流?