ostream实现(str,f,cout)的工作方式略有不同 [英] ostream implementations (str, f, cout ) work slightly differently

查看:84
本文介绍了ostream实现(str,f,cout)的工作方式略有不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



你好,

我现在正在写c ++几个月。我想我现在已经知道了这个语言,但还不是stl的所有棘手的事情。


对于没有使用mfc的内核我正在编写序列化。出于某些原因,我想在我的

上实现它。

一个目标是使其对版本更改和损坏的文件具有强大的功能。这意味着,当一个对象不能反序列化时,应该跳过它。剩下的文档的有效性可以在以后验证。

为此我用一个标识该类的代码开始每个对象。工厂可以使用它来构造类的b $ b,为它提供序列化流的节点。

下一个是对象的大小。这样,如果出现问题,可以跳过该对象。早期版本

可能不包含类的所有变量,后者可能包含太多变量。类的序列化器或反序列化器

方法将有适当的默认值。

问题是在对象已经序列化之前,大小当然是未知的。为了表现

很重要我不希望在序列化之前进行一种大小查询,我不希望有什么东西可以序列化

到另一个地方,然后复制到真正的流。即使我的文件可能不会太大,我也不会知道将来会发生什么。

所以我的方法是保存当前的流在序列化之前的位置,然后来回跳转,把大小放在正确的位置。

我的第一个问题是:现在有人有多贵吗?我想不是太多,但我真的不知道

流的实现。

然后我看到它有时不像预期的那样表现。

好​​的,cout可以获得它只返回零的位置。没问题,因为序列化到cout并不是真的有用

给我。

有人可能会问我为什么要使用这个通用的ostream来序列化。好吧,我不知道它将要做什么

以后:保存文档,交易(撤销/重做),网络通信或只是与另一个进程,拖放......

因此我认为一般的apporoach是可以的。

现在文件流有时将第一个int大小放在第一位但是移动了大约一个字节。这是
奇怪,丑陋,它破坏了实际的数据。但这取决于实际数据的大小。它会发生,如果它是小bb,并且取决于它的大小是奇数还是偶数!?!我试图通过初始化初始化

字符串来克服这一点,因为它将是众所周知的,因此不需要大小信息。但在这种情况下,问题出现在这个

初始化字符串之后。因此,我使用seekp()方法跳转。

strstream有点相似,但第一个数据值没有损坏。但整个数据移动了一点

:-(。

所以,如果一个人真的熟悉流:我只想知道我需要考虑什么使我的代码

安全地工作。它不仅不能工作,而且无论如何在任何机器和编译器上都不同于我的

VS.net2003编译器
感谢您提前的帮助。我试图发布消息来源,但服务器被拒绝了。可能太大了。询问是否需要

我会发布它

欢呼

Ingo


Hi there,
I am writing c++ for some months now. I think I know the language now, but not yet all the tricky things of stl.

For a kernel which is not using mfc I am writing a serialization. For some reasons I want to implement it on my
own.
One goal is to make it robust against version changes and corrupted files. That means, when an object cannot
be deserialized, it should be skipped. The validity of the remaining document can be verified later.
For this purpose I begin every object with a code that identifies the class. A factory can use it to construct
the class, giving it the node of the serialized stream.
The next is the size of the object. This way the object can be skipped if something is wrong. An earlier version
might not contain all the variables for a class, a later one may contain too much. The serializer or deserializer
methods of the class will have apropriate default values at hand.
The problem is that the size is of course unknown before th object is already serialized. For performance
matters I dont want to have a kind of size inquiry before the serialization and I dont want something to serialize
to another place and then copy to the real stream. Even though my files might not be going to be soo big, I
don''t know what comes in the future.
So my way is to save the current stream position before the serialization, and then jump back and forth to put
the size in the right place.
My first question is: Does anyone now how costly that is? I guess not too much, but I don''t really knwo about
the implementation of the streams.
Then I see that it sometimes doesn''t behave as expected.
Ok, the cout can get the position it returns only zero. No problem since serializing to cout is not really useful
to me.
one might ask why I want to use this general ostream to serialize to. Well I dont know what it is going to do
later: saving docs, transaction ( undo/redo), network communication or just with another process, dragn drop...
therefore I think a general apporoach is ok.
Now the file stream sometime puts the very first int size not into the first place but shifted about one byte. THis
is weird, ugly and it corrupts the actual data. But it depends on the size of the actual data. It happens if it is
small, and depends on wether the size is odd or even !?! I tried to overcome this with an initializing "initialize"
string wich will be known and therefore needs no size info. But in this case the problem appears after this
initilaizing string. So it is something with my jumping around with the seekp() method.
The strstream is a little similar but the first data value is not corrupted. But the whole data is shifted by one bit
:-(.
So if one is really familiar with the streams: I just want to know what do I have to consider to make my code
work securely. It should not only work no, but in any case on any machine and on compilers different than my
VS.net2003 compiler
thanks for your help in advance. I tried to post source, but the server rejected. Probably too big. Ask if
necessary and I will post it
cheers
Ingo

推荐答案



" Ingo Nolden"< nu ** **********@SPAMgmx.de>在留言中写道

news:c6 ********** @svr7.m-online.net ...

"Ingo Nolden" <nu************@SPAMgmx.de> wrote in message
news:c6**********@svr7.m-online.net...

你好,
我现在正在写c ++几个月。我想我现在知道这个语言,但是
还不是stl的所有棘手的事情。
对于没有使用mfc的内核我正在编写序列化。对于一些
的原因我想实现我自己。
一个目标是使其能够抵御版本更改和损坏的文件。
这意味着,当一个对象无法反序列化时,应该跳过它。剩余的
文档的有效性可以在以后验证。为此,我使用标识
类的代码开始每个对象。工厂可以使用它来构造类,为它提供序列化流的节点。
接下来是对象的大小。这样,如果
出错,则可以跳过该对象。早期版本可能不包含类的所有变量,后面的版本可能包含
太多。该类的序列化程序或反序列化方法将具有适当的默认值。
问题是在对象已经序列化为
之前,大小当然是未知的。对于性能问题,我不希望在
序列化之前进行一种大小查询,我不想将某些内容序列化到另一个地方,然后复制到实际流。即使我的文件
可能不会太大,我也不知道将来会发生什么。
所以我的方法是在序列化之前保存当前的流位置,
然后来回跳动以将尺寸放在正确的位置。
我的第一个问题是:现在有人有多贵吗?我猜不会太多b $ b,但我真的不知道流的实现。
然后我看到它有时不会像预期的那样表现。
好的, cout可以获得它仅返回零的位置。没问题,因为
序列化到cout对我来说并不是真的有用。
有人可能会问我为什么要使用这个通用的ostream来序列化。好吧
我不知道以后要做什么:保存文档,事务(撤销/重做),网络通信或
只是与另一个进程,dragn drop ...因此我认为一般的apporoach是好的。
现在文件流有时将第一个int大小放入
的第一个位置但是移动了大约一个字节。这很奇怪,很丑,它破坏了实际的数据。但这取决于实际数据的大小
。它会发生,如果它很小,取决于大小是奇数还是偶数!?!

Hi there,
I am writing c++ for some months now. I think I know the language now, but not yet all the tricky things of stl.
For a kernel which is not using mfc I am writing a serialization. For some reasons I want to implement it on my own.
One goal is to make it robust against version changes and corrupted files. That means, when an object cannot be deserialized, it should be skipped. The validity of the remaining document can be verified later. For this purpose I begin every object with a code that identifies the class. A factory can use it to construct the class, giving it the node of the serialized stream.
The next is the size of the object. This way the object can be skipped if something is wrong. An earlier version might not contain all the variables for a class, a later one may contain too much. The serializer or deserializer methods of the class will have apropriate default values at hand.
The problem is that the size is of course unknown before th object is already serialized. For performance matters I dont want to have a kind of size inquiry before the serialization and I dont want something to serialize to another place and then copy to the real stream. Even though my files might not be going to be soo big, I don''t know what comes in the future.
So my way is to save the current stream position before the serialization, and then jump back and forth to put the size in the right place.
My first question is: Does anyone now how costly that is? I guess not too much, but I don''t really knwo about the implementation of the streams.
Then I see that it sometimes doesn''t behave as expected.
Ok, the cout can get the position it returns only zero. No problem since serializing to cout is not really useful to me.
one might ask why I want to use this general ostream to serialize to. Well I dont know what it is going to do later: saving docs, transaction ( undo/redo), network communication or just with another process, dragn drop... therefore I think a general apporoach is ok.
Now the file stream sometime puts the very first int size not into the first place but shifted about one byte. THis is weird, ugly and it corrupts the actual data. But it depends on the size of the actual data. It happens if it is small, and depends on wether the size is odd or even !?!




可能你没有以二进制模式打开文件。绝对定位

的文件流是不可能的,除非你这样做


例如


ostream文件(" somefile.txt",ios_base :: out | ios_base :: binary);


john



Probably you have not opened the file in binary mode. Absolute positioning
of file streams is not possible unless you do this

E.g.

ostream file("somefile.txt", ios_base::out|ios_base::binary);

john


>
例如

ostream文件(" somefile.txt",ios_base :: out | ios_base :: binary);
E.g.

ostream file("somefile.txt", ios_base::out|ios_base::binary);




当然。


john



ofstream of course.

john


谢谢


有时候事情是简单。

文件正在按预期工作。


strstream也有这样的sthing作为二进制模式吗?或者是否有一些与我可用于二进制数据的

strstream相关的模板。它的工作几乎完美,偶尔它会移动一个字节。我可以找到一种方法来同步它,即使我不认为这是应该怎么做的。


欢呼


John Harrison

写道:< c6 ************ @ ID-196037.news.uni-berlin .de>
thank you

some times things are simple.
the file thing is working now as expected.

Is there such sthing as a binary mode for the strstream, too? Or is there some template related to the
strstream that I can use for binary data. It works nearly perfect just occasionally it shifts by a byte. Of coure I
could find a way to synchronize it somehow, even though I dont think this is how it should be done.

cheers

"John Harrison"
wrote in message: <c6************@ID-196037.news.uni-berlin.de>

例如

ostream文件(" somefile.txt",ios_base :: out | ios_base :: binary);

E.g.

ostream file("somefile.txt", ios_base::out|ios_base::binary);



当然。

john



ofstream of course.

john



这篇关于ostream实现(str,f,cout)的工作方式略有不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆