By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,915 Members | 1,268 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,915 IT Pros & Developers. It's quick & easy.

memory stream?

P: n/a
I am writing a program that involves reading in lots of (ASCII) data,
manipulating it, and then outputting some more. I am using ifstream and
ofstream.

Since I am reading a line, and writing a line at a time, my code can be
quite slow.

Is it possible, to use a memory stream, such that I read the entire
file in to memory first, and then can use it, just like a file stream?
Similarly, can I write to memory, just like I would be writing to a
file, and then write the entire data to disk?

Many thanks.

Jul 26 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Użytkownik andrew napisał:
Is it possible, to use a memory stream, such that I read the entire
file in to memory first, and then can use it, just like a file stream?
Similarly, can I write to memory, just like I would be writing to a
file, and then write the entire data to disk?

Many thanks.


Yes, and it's easy. Just use stringstream. You can't construct
stringstream out of ifstream's buffer, but - as with all streams - you
can send the whole buffer to it.

#include <sstream>
#include <fstream>
using namespace std;

int main()
{
ifstream in("test.txt");
stringstream ss;
ss << in.rdbuf(); // Put the whole file into ss
// use ss as every other stream...
}

Stefan
Jul 26 '05 #2

P: n/a


andrew wrote:

[]
Is it possible, to use a memory stream, such that I read the entire
file in to memory first, and then can use it, just like a file stream?
Similarly, can I write to memory, just like I would be writing to a
file, and then write the entire data to disk?


[]

You can't do it with std::stringstream. Alternatively, you can add
std::ostream interface to an array.
http://groups-beta.google.com/group/...72eaedb142debc

Jul 27 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.