如何将cin和cout重定向到文件?

时间:2012-04-14 02:19:20

标签: c++

如何将cin重定向到in.txtcout重定向到out.txt

7 个答案:

答案 0 :(得分:183)

以下是您要执行的操作示例。阅读注释以了解代码中的每一行。我用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's缓冲区设置为in.rdbuf(),然后返回与std::cin关联的旧缓冲区。可以使用std::cout - 或任何来完成同样的事情。

希望有所帮助。

答案 1 :(得分:80)

只需写下

#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
    freopen("output.txt","w",stdout);
    cout<<"write in file";
    return 0;
}

答案 2 :(得分:15)

假设您的编译程序名称是x.exe 和$是系统shell或提示符

$ x <infile >outfile 

将从infile获取输入并输出到outfile。

答案 3 :(得分:14)

以下是阴影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。所以订单很重要。

另见Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);

对于单个文件的范围,Std io流也很容易被遮蔽,这对于竞争性编程很有用:

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

另请参阅How do you properly use namespaces in C++?Why is "using namespace std" considered bad practice?How to resolve a name collision between a C++ namespace and a global function?

答案 4 :(得分:0)

尝试将 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;
}

阅读全文Use std::rdbuf to Redirect cin and cout

答案 5 :(得分:0)

如果你的输入文件是in.txt,你可以使用freopen将stdin文件设置为in.txt

freopen("in.txt","r",stdin);

如果你想对你的输出做同样的事情:

freopen("out.txt","w",stdout);

这将适用于 std::cin(如果使用 C++)、printf 等...

这也将帮助您在 clion、vscode 中调试代码

编辑
如果你想重置标准输入

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

答案 6 :(得分:-1)

C++ 中的 I/O 重定向

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