Hi All
This is more a discussion than a problem. :)
I’m working on a small project that needs to be done in C++.
Part of it requires that I maintain an array of file streams so that I can open serveral files at one time, read, write exchange data as often as I need to and close them when done.
So dusting of some of my old C++ books (these days my coding is done all in C++ CLI/CLR) and read up on the fstream class. Ah! yes istream for reading ostream for writing, bugger two streams! What to do?
Back in the old C days I could use:

FILE  *fp[20];
fp[0] = fopen(“filename”, “rb+”);

Nowadays in CLR

Array<FileStream^>^ fp = gcnew array<FileStream^>(20);
fp[0] =  gcnew FileStream(“filename”, FileMode::Open, FileAccess::ReadWrite);

Is seems C++ does not have a stream class that supports a read/write in the same open mode. Or should I do some more reading :)
I’ve knock up some testing code using the older C style “FILE” and this worked great, just as I expected it would.
Question
Is it “acceptable” amongst even the purest of C++’s programmers to mix old C style code in with the OOP style of C++, or should I re-think my design working around the C++ fstream class.
Open for discussion.
Milton
:)

Recommended Answers

All 2 Replies

std::stringstream
std::fstream

vs

std::ostringstream
std::ofstream
std::ifstream
etc...

Edit: It seems I misunderstood your post, my apologies.
Personally, I'd do it the C way, because it would be easier for me. That's just my taste though, you should do whatever is easiest for you.

>Is seems C++ does not have a stream class that supports a read/write in the same open mode.
You should do a little more reading on open modes with the fstream class. Read/write mode is both supported and common. The only caveat is the same as with C streams: when switching between read and write mode, there needs to be an intervening flush or seek.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.