जवाबों:
यहाँ एक उदाहरण है कि आप क्या करना चाहते हैं। कोड में प्रत्येक पंक्ति क्या करती है, यह जानने के लिए टिप्पणियों को पढ़ें। मैंने इसे अपने पीसी पर 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- या उस मामले के लिए किसी भी धारा के साथ ।
उम्मीद है की वो मदद करदे।
inऔर outसे और लिखने के लिए पढ़ने के लिए, in.txtऔर out.txtक्रमशः। इसके अलावा, फ़ाइलें स्वचालित रूप से जब बंद कर दिया जाएगा inऔर outक्षेत्र से बाहर चले जाते हैं।
freopenक्योंकि stdoutअगर मैं उपयोग करता हूं तो मुझे अपनी पीठ नहीं मिल सकती है freopen। stackoverflow.com/questions/26699524/…
बस लिखें
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
freopen("output.txt","w",stdout);
cout<<"write in file";
return 0;
}
stdout, नहीं cout।
std::sync_with_studio(false);, हालाँकि डिफ़ॉल्ट रूप से यह सेट है true।
ofstream out("out.txt"); cout.rdbuf(out.rdbuf());- केवल एक अतिरिक्त रेखा, और यह पोर्टेबल है। बहुत सरल नहीं है :)
यहाँ छायांकन के लिए एक छोटा कोड स्निपेट है जो प्रोग्रामिंग प्रतियोगिताओं के लिए उपयोगी है।
#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 ++ नाम स्थान और वैश्विक फ़ंक्शन के बीच एक नाम टकराव को कैसे हल करें?
अपने संकलन का नाम मान लेना x.exe है और $ सिस्टम शेल या प्रॉम्प्ट है
$ x <infile >outfile
शिशु से इनपुट लेगा और आउटफिट में आउटपुट करेगा।
रीडायरेक्ट करने के लिए इस प्रयास करें अदालत फाइल करने के लिए।
#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