14 NİSAN 2012, CUMARTESİ
Nasıl cin Yönlendirme ve dosyalar için aynı mı?
in.txt
out.txt
cout
cin
yönlendirmek için umarım. Bunu nasıl lütfen?
CEVAP
14 NİSAN 2012, CUMARTESİ
Burada ne yapmak istediğinizi çalışan bir örnek. Açıklamalar kod her satırın ne yaptığını bilmek için okuyun. 4.6.1; gayet iyi çalışıyor gcc benim pc ile denedim.
#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
}
OlabilirKaydetveyönlendirmeksadece bir çizgi gibi
auto cinbuf = std::cin.rdbuf(in.rdbuf()); //save and redirect
Burada std::cin.rdbuf(in.rdbuf())
std::cin's
10 ** tampon ayarlar ve sonra eski tamponu std::cin
ile ilişkili verir. Aynı std::cout
- ile yapılabilir; ya da herhangi birstreambu konuda.
Bu yardımcı olur umarım.
Bunu Paylaş:
Nasıl stderr yönlendirmek ve bash aynı...
Nasıl jQuery Ajax çağrısından sonra bi...
Nasıl ilerleme iletişim ve arka plan i...
Sudo nasıl bir konuma çıkış yönlendirm...
Nasıl PHP bir yönlendirme yapmak için?...