我应该在哪里使用 iostream 类?
正如我们所知,在 C++ 中,我们有 iostream 类,它继承自 istream(basic_istream) 和 ostream (basic_ostream)。在每一本 C++ 书籍中,您都可以找到,使用 iostream 类对象,您可以读取和写入同一流。但我确实没有看到任何解释或例子来理解为什么我应该使用如此奇怪的想法。我真的不知道为什么我需要写入某个流而不是从中读取:(。
你能解释一下我什么时候需要这样的构造吗?我认为使用这样的构造一定有严重的原因(不要忘记仅对于 iostream 声明,我们使用虚拟继承和多重继承)。
另外,当我尝试编写一个使用 fsteram(iostream 的派生)的简单代码时,我发现它不起作用,这是我所期望的。我的代码:
#include <fstream>
using namespace std;
int main()
{
fstream fstr("somefile.txt",fstream::in|fstream::out);//fstream is deriveted from iosteram
int n;
fstr>>n;//reading n (WORKS FINE !!!).
fstr.flush();
//trying to print Hello to the same file
fstr<<"Hello"<<endl;// NOT WORKING!!!!!!!
fstr.flush();
return 0;
}
那么你能告诉我为什么这段代码可以从文件中读取但不能写入内容吗
??? 请告诉我为什么我们需要 iosteram 类,为什么 isteram 和 ostream 还不够,以及如何使用它。
谢谢并抱歉我的英语:)。
PS也许这个问题很原始,但请回答我。
编辑:我的代码现在可以工作了。感谢穆尔卡。
As we know in C++ we have class iostream, which is inherited from istream(basic_istream) and ostream (basic_ostream). In every C++ book you can find, that with iostream class object you can read and write to the same stream. But I realy haven't see any explanation or example to understand why should I use such a strange think. I really don't know why should I need to write to some stream and than read from it :(.
Could you explain me when I should need such construction? I think there must be serous reason for using such construction(don't forget that only for iostream declaration we are using virtual inheritance and multiple inheritance).
Also when I try to write a simple code, which is using fsteram(derivative of iostream) I find, that its not working in way, which I expect. Here is my code:
#include <fstream>
using namespace std;
int main()
{
fstream fstr("somefile.txt",fstream::in|fstream::out);//fstream is deriveted from iosteram
int n;
fstr>>n;//reading n (WORKS FINE !!!).
fstr.flush();
//trying to print Hello to the same file
fstr<<"Hello"<<endl;// NOT WORKING!!!!!!!
fstr.flush();
return 0;
}
So could you tell me why this code can read from file and can't write something to it????
Resume:
Please tell me why we need class iosteram and why isteram and ostream arn't enought and how to use it.
Thanks and sorry for my english :).
P.S. Probably this question is to primitive, but please answer me.
Edit: My code is now working. Thanks to Murka.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
IIRC 你需要先进行查找,然后才能写入,不要问为什么。
还添加了代码来清除它可能引发的任何标志。
IIRC you need to do a seek before you can write, don't ask why.
Also added code to clear any flags it might raise.
您可能需要一个更新或编辑现有文件的程序的这种能力:它必须读取已存在的内容,找到要更新或编辑的部分,并且必须将更新(更改)写入文件。
You might need that ability for a program that updates or edits an existing file: it must read what is already present, to locate the part to be updated or edited, and must write the update (alteration) to the file.
您可能想要读取和写入同一流,因为该流执行类型转换,如 std::stringstream。您还可以对允许读取和写入的数据源(例如套接字或内存缓冲区)进行 iostream 抽象。
You might want to read from and write to the same stream because the stream performs type conversions, like std::stringstream. You could also have iostream abstractions over data sources that permit both reading and writing- such as a socket or an in-memory buffer.
很少。在这里查看我的回答:
https://stackoverflow.com/questions/4517299/is- fstream-better-than-iostream-in-c/4517883#4517883
编辑:
如果你认为你不需要它,那么你就不需要它。
Rarely. See my answer here:
https://stackoverflow.com/questions/4517299/is-fstream-better-than-iostream-in-c/4517883#4517883
Edited:
If you don't think you need it, you don't need it.