cin
重定向到 in.txt
,并将 cout
重定向到 out.txt
?cin
重定向到 in.txt
,并将 cout
重定向到 out.txt
?这是一个可行的示例,可以完成你想做的事情。阅读注释以了解代码中每行的作用。我已在我的pc上使用gcc 4.6.1进行了测试;它能正常工作。
#include <iostream>
#include <fstream>
#include <string>
void f()
{
std::string line;
while(std::getline(std::cin, line)) //input from the file in.txt
{
std::cout << line << "\n"; //output to the file out.txt
}
}
int main()
{
std::ifstream in("in.txt");
std::streambuf *cinbuf = std::cin.rdbuf(); //save old buf
std::cin.rdbuf(in.rdbuf()); //redirect std::cin to in.txt!
std::ofstream out("out.txt");
std::streambuf *coutbuf = std::cout.rdbuf(); //save old buf
std::cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!
std::string word;
std::cin >> word; //input from the file in.txt
std::cout << word << " "; //output to the file out.txt
f(); //call function
std::cin.rdbuf(cinbuf); //reset to standard input again
std::cout.rdbuf(coutbuf); //reset to standard output again
std::cin >> word; //input from the standard input
std::cout << word; //output to the standard input
}
您可以使用以下一行代码完成保存并重定向:
auto cinbuf = std::cin.rdbuf(in.rdbuf()); //save and redirect
这里std::cin.rdbuf(in.rdbuf())
将std::cin
的缓冲区设置为in.rdbuf()
并返回与std::cin
关联的旧缓冲区。同样的操作也可以用于std::cout
或者任何其他流。
in
和 out
来分别读取和写入文件,分别对应于 in.txt
和 out.txt
。此外,当 in
和 out
超出作用域范围时,文件将自动关闭。 - Nawazfreopen
,因为如果我使用freopen
,我将无法再次获取我的stdout
。https://stackoverflow.com/questions/26699524/how-to-write-in-stdout-after-using-freopen - xxks-kkk只需写作
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
freopen("output.txt","w",stdout);
cout<<"write in file";
return 0;
}
ofstream out("out.txt"); cout.rdbuf(out.rdbuf());
- 只多了一行代码,而且它是可移植的。虽然不会简单太多 :) - nevelis这是一个有用的编程竞赛 cin/cout 输入输出重定向的简短代码示例:
#include <bits/stdc++.h>
using namespace std;
int main() {
ifstream cin("input.txt");
ofstream cout("output.txt");
int a, b;
cin >> a >> b;
cout << a + b << endl;
}
这样做的额外好处是普通的 fstream 比同步的 stdio 流更快。但是这仅适用于单个函数的范围。
全局 cin/cout 重定向可以写成:
#include <bits/stdc++.h>
using namespace std;
void func() {
int a, b;
std::cin >> a >> b;
std::cout << a + b << endl;
}
int main() {
ifstream cin("input.txt");
ofstream cout("output.txt");
// optional performance optimizations
ios_base::sync_with_stdio(false);
std::cin.tie(0);
std::cin.rdbuf(cin.rdbuf());
std::cout.rdbuf(cout.rdbuf());
func();
}
请注意,ios_base::sync_with_stdio
还会重置std::cin.rdbuf
。因此顺序很重要。
另请参阅《ios_base::sync_with_stdio(false); cin.tie(NULL);的意义》
对于单个文件的范围,标准输入输出流也可以很容易地被遮蔽,这在竞赛编程中很有用:
#include <bits/stdc++.h>
using std::endl;
std::ifstream cin("input.txt");
std::ofstream cout("output.txt");
int a, b;
void read() {
cin >> a >> b;
}
void write() {
cout << a + b << endl;
}
int main() {
read();
write();
}
但在这种情况下,我们必须逐个选择 std
声明,并避免使用 using namespace std;
,因为它会导致歧义错误:
error: reference to 'cin' is ambiguous
cin >> a >> b;
^
note: candidates are:
std::ifstream cin
ifstream cin("input.txt");
^
In file test.cpp
std::istream std::cin
extern istream cin; /// Linked to standard input
^
另请参见如何正确使用C++命名空间?,为什么被认为“using namespace std”是不良实践?以及如何解决C++命名空间和全局函数之间的名称冲突?
假设您编译的程序名为x.exe,$表示系统shell或提示符
$ x <infile >outfile
将从输入文件中获取输入,并输出到输出文件中。
尝试以下方法将cout重定向到文件。
#include <iostream>
#include <fstream>
int main()
{
/** backup cout buffer and redirect to out.txt **/
std::ofstream out("out.txt");
auto *coutbuf = std::cout.rdbuf();
std::cout.rdbuf(out.rdbuf());
std::cout << "This will be redirected to file out.txt" << std::endl;
/** reset cout buffer **/
std::cout.rdbuf(coutbuf);
std::cout << "This will be printed on console" << std::endl;
return 0;
}
阅读完整文章:使用std::rdbuf重定向cin和cout
cin
和 cout
。抱歉,如果只重定向 cin
或只重定向 cout
,大多数程序员都能剥离掉那个他们不需要的答案的一半。 - Kai Petzkefreopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
这将适用于 std::cin(如果使用 c++),printf 等...
这也将帮助您在 clion、vscode 中调试代码
编辑
如果您想重置 stdin
fclose(stdin);
stdin = fdopen(0, "r"); //reopen: 0 is file descriptor of std input
并重置标准输出
fclose(stdout);
stdout = fdopen(1, "w"); //reopen: 1 is file descriptor of std output
stdout
? - ex1led接受的答案展示了正确的重定向 cin
和 cout
的方式。您需要构造另一个流对象,其生命周期超过 cin
或 cout
。如果您想编写一个类似于 freopen
的函数,则可以为每个要重定向的流分配一个数组,以保存已分配的流对象。
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
template<typename>
struct fstream_traits { };
template<typename CharT, typename Traits>
struct fstream_traits<basic_istream<CharT, Traits>> { using type = basic_ifstream<CharT, Traits>; };
template<typename CharT, typename Traits>
struct fstream_traits<basic_ostream<CharT, Traits>> { using type = basic_ofstream<CharT, Traits>; };
template <typename Stream>
void redirect(Stream& str, string filename)
{
using fstream_type = typename fstream_traits<Stream>::type;
static int index = std::ios_base::xalloc();
if (str.pword(index) == nullptr)
{
str.pword(index)= new vector<ios_base*>{};
str.register_callback([](ios_base::event event, std::ios_base& stream, int index) {
if (event == ios_base::erase_event)
{
for (auto fs : *(vector<ios_base*>*)stream.pword(index))
delete fs;
delete (vector<ios_base*>*)stream.pword(index);
}
}, index);
}
vector<ios_base*>* list = (vector<ios_base*>*)str.pword(index);
list->push_back(new fstream_type{filename});
str.rdbuf(dynamic_cast<fstream_type*>(list->back())->rdbuf())->~basic_streambuf();
}
int main()
{
redirect(cout, "out.txt");
cout << "Redirected text!";
return 0;
}
istream
/ostream
而不是Stream
,则模板和别名不是必需的。C++中的输入/输出重定向
https://www.geeksforgeeks.org/io-redirection-c/
// Cpp program to redirect cout to a file
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
fstream file;
file.open("cout.txt", ios::out);
string line;
// Backup streambuffers of cout
streambuf* stream_buffer_cout = cout.rdbuf();
streambuf* stream_buffer_cin = cin.rdbuf();
// Get the streambuffer of the file
streambuf* stream_buffer_file = file.rdbuf();
// Redirect cout to file
cout.rdbuf(stream_buffer_file);
cout << "This line written to file" << endl;
// Redirect cout back to screen
cout.rdbuf(stream_buffer_cout);
cout << "This line is written to screen" << endl;
file.close();
return 0;
}