473,397 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Setting a file stream to std[in/out/err]?

In the stdio library, supported by C, we can set a file pointer to
stdin or stdout ot stderr (e.g. FILE* fp = stdin;). Can we do
something similar with ifstream and ofstream in C++? i.e. can we set
the ifstream to stdin and the ofstream to stdout or stderr? If the
answer is yes, please provide a code snippet.

Thanks,
Masood

Aug 15 '07 #1
2 9966
On 15 Srp, 06:31, masood.iq...@lycos.com wrote:
In the stdio library, supported by C, we can set a file pointer to
stdin or stdout ot stderr (e.g. FILE* fp = stdin;). Can we do
something similar with ifstream and ofstream in C++? i.e. can we set
the ifstream to stdin and the ofstream to stdout or stderr? If the
answer is yes, please provide a code snippet.

Thanks,
Masood
Do all your stuff in some function or method:

#include <iostream>
#include <fstream>

void DoSomethingWithStream(std::ostream& os)
{
os << "Do something here\n";
}

int main()
{
// You can call DoSomethingWithStream with ostream as well as with
ofstream
std::ofstream ifs("filename", std::ios::in);
DoSomethingWithStream(ifs);
ifs.close();

DoSomethingWithStream(std::cout);
}

Aug 15 '07 #2
On Aug 15, 7:31 am, masood.iq...@lycos.com wrote:
In the stdio library, supported by C, we can set a file pointer to
stdin or stdout ot stderr (e.g. FILE* fp = stdin;). Can we do
something similar with ifstream and ofstream in C++? i.e. can we set
the ifstream to stdin and the ofstream to stdout or stderr? If the
answer is yes, please provide a code snippet.

Thanks,
Masood

std::ofstream file("sil.txt",ios::out);

std::cout.rdbuf(file.rdbuf());

std::cout<<"this goes to the file"<<std::endl;

Aug 15 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gigi | last post by:
Hello, I have a byte array and i must send(to blocks) to remote class In the remote class i write a file stream...... How? In my sample i send stream...
5
by: Alexandros Frantzis | last post by:
Hello! Is there a standard way to find the reason a file stream open failed (eg file doesn't exist, access rights)? Thanks.
5
by: Grumble | last post by:
Hello all, According to P.J. Plauger's C++ library reference, std::ifstream ifs(filename); will initialize several objects, then call sb.open(filename, ios_base::in);
3
by: Anne-Marte | last post by:
Hi I simply don't understand how to read a simple file using std::istream. How do I open a file for reading with istream?? Anne-Marte
4
by: Anton Pervukhin | last post by:
Hi everybody! I have a small problem regarding the initialization of pointer to the file stream under some conditions. Imagine a class which has a pointer to output file stream and some...
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
7
by: Lee | last post by:
Hi, I'm a stream virgin and am attempting to output strings to a file. My approach is to write the string initially to a 'stringstream' and only when complete write the stringstream to the file...
9
by: sherifffruitfly | last post by:
Hi, I've a got a little (exercise) program that reads data from a file and puts it into struct members. I run into trouble when one of the data pieces is comprised of several words (eg "john...
5
by: cnoe | last post by:
I need to program this. I'm not asking any code, but an idea from you would be helpful. Thanks. I open a binary file stream, which has millions or trillions of 1's & 0's some thing like this...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.