As is known in C ++, we have the iostream class, which is inherited from istream (basic_istream) and ostream (basic_ostream). In every C ++ book you can find that with an iostream class object you can read and write to the same stream. But I really do not see any explanation or examples to understand why I should use such a strange opinion. I really don’t know why I need to write to some stream and how to read from it :(.
Could you explain to me when I need such a design? I think that there must be a serious reason for using this design (do not forget that only for iostream declaration we use virtual inheritance and multiple inheritance).
Also, when I try to write simple code that uses fsteram (derived from iostream), I find that it does not work in the path that I expect. Here is my code:
#include <fstream>
using namespace std;
int main()
{
fstream fstr("somefile.txt",fstream::in|fstream::out);
int n;
fstr>>n;
fstr.flush();
fstr<<"Hello"<<endl;
fstr.flush();
return 0;
}
So could you tell me why this code can read from a file and cannot write something?
Summary: Please tell me why we need the iosteram class and why isteram and ostream arn't enought and how to use it.
Thanks and sorry for my english :).
PS This question is probably primitive, but please answer me.
Edit: my code now works. Thanks Murka.
source
share