फ़ाइलों पर सिने और कटआउट को पुनर्निर्देशित कैसे करें?


133

मैं कैसे और क्या करने के cinलिए पुनर्निर्देशित कर सकते हैं ?in.txtcoutout.txt


8
एक स्ट्रिंग पर पुनर्निर्देशित सिनेमा: stackoverflow.com/questions/4925150/redirect-cin-to-a-string - एक स्ट्रिंग के लिए पुनर्निर्देशन: stackoverflow.com/questions/1162068/…
डॉ। कैमरून

जवाबों:


219

यहाँ एक उदाहरण है कि आप क्या करना चाहते हैं। कोड में प्रत्येक पंक्ति क्या करती है, यह जानने के लिए टिप्पणियों को पढ़ें। मैंने इसे अपने पीसी पर 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- या उस मामले के लिए किसी भी धारा के साथ ।

उम्मीद है की वो मदद करदे।


4
क्या मुझे मानक IO पर सिने और कॉट रीसेट करने से पहले फ़ाइलों को बंद करने की आवश्यकता है?
updogliu

3
@updogliu: नहीं आप चाहते हैं, तो आप उपयोग कर सकते हैं inऔर outसे और लिखने के लिए पढ़ने के लिए, in.txtऔर out.txtक्रमशः। इसके अलावा, फ़ाइलें स्वचालित रूप से जब बंद कर दिया जाएगा inऔर outक्षेत्र से बाहर चले जाते हैं।
नवाज

मुझे यह समाधान पसंद है freopenक्योंकि stdoutअगर मैं उपयोग करता हूं तो मुझे अपनी पीठ नहीं मिल सकती है freopenstackoverflow.com/questions/26699524/…
xxks-kkk

88

बस लिखें

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

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

14
वह पुनर्निर्देशन है stdout, नहीं cout
updogliu

5
यह प्रिंटफ को भी रीडायरेक्ट करेगा, जो कुछ मामलों में अच्छी बात हो सकती है।
JDiMatteo

5
@AkshayLAradhya तब नहीं जब आप सेट करते हैं std::sync_with_studio(false);, हालाँकि डिफ़ॉल्ट रूप से यह सेट है true
vsoftco

2
@ P @emyslŠťastný क्यों?
रेगिगुएटर

4
यदि उत्तर कार्यक्षमता में बराबर थे, तो इसका C ++ समकक्ष होगा ofstream out("out.txt"); cout.rdbuf(out.rdbuf());- केवल एक अतिरिक्त रेखा, और यह पोर्टेबल है। बहुत सरल नहीं है :)
नेवेलिस

19

यहाँ छायांकन के लिए एक छोटा कोड स्निपेट है जो प्रोग्रामिंग प्रतियोगिताओं के लिए उपयोगी है।

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

यह अतिरिक्त लाभ देता है कि सादे fstreams सिंक किए गए stdio धाराओं की तुलना में तेज़ हैं। लेकिन यह केवल एकल फ़ंक्शन के दायरे के लिए काम करता है।

वैश्विक सिनेमा / कूट पुनर्निर्देशन को इस प्रकार लिखा जा सकता है:

#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 (झूठा); cin.tie (शून्य);

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
                   ^

यह भी देखें कि आप C ++ में नामस्थान का ठीक से उपयोग कैसे करते हैं? , क्यों "नेमस्पेस स्टैड का उपयोग करना" बुरा व्यवहार माना जाता है? और C ++ नाम स्थान और वैश्विक फ़ंक्शन के बीच एक नाम टकराव को कैसे हल करें?


15

अपने संकलन का नाम मान लेना x.exe है और $ सिस्टम शेल या प्रॉम्प्ट है

$ x <infile >outfile 

शिशु से इनपुट लेगा और आउटफिट में आउटपुट करेगा।


यह C ++ संबंधित नहीं है और किसी भी गैर-तुच्छ उदाहरण में विफल रहता है, उदाहरण के लिए जब आपका प्रोग्राम कंसोल को लिखने वाले बच्चे की प्रक्रियाओं को जन्म देता है। जब मैंने इस तरह के पुनर्निर्देशन की कोशिश की तो कम से कम यही समस्या आई है, इसलिए मैं यहां हूं।
आश्रममुन

5

रीडायरेक्ट करने के लिए इस प्रयास करें अदालत फाइल करने के लिए।

#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 to Redirect Cin and cout


1
लगभग 6 साल पहले (2012 में) इस सवाल का जवाब दिया गया था, फिर भी आपने 2018 में अब एक उत्तर जोड़ दिया है। आपका उत्तर स्वीकृत उत्तर के समान है। तो मैं सोच रहा था कि जब आप कुछ भी नया जोड़ने के लिए नहीं थे तो आपने यह पोस्ट क्यों किया ?
नवाज

मेरा उत्तर केवल विशेष रूप से कॉउट संस्करण पर प्रकाश डालता है और विस्तृत उत्तर नीचे दिए गए लिंक में दिया गया है।
हसीबी मीर

आपके उत्तर में नया क्या है जो स्वीकृत उत्तर में मौजूद नहीं है ?
नवाज

2
मेरे उत्तर में
कॉट
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.