fstream的put和get指针之间有区别吗? [英] Is there a difference between the put and get pointer of a fstream?

查看:161
本文介绍了fstream的put和get指针之间有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下问题感到有些困惑.我正在使用带有二进制文件的fstream.最初,我想做的是从刚刚写入文件的文件中读取一个值.所以我需要一个in | out模式.

到目前为止,我一直认为,在执行read()时会使用并提高流的get指针,并在使用write()时会相应地使用put指针(我之前只使用过ifstream和ofstream).

但是,正如以下测试所示,似乎它们实际上始终指向同一位置.因此,在执行read(),write或seek()操作时,两个指针均会前进.

Im a little confused about the following issue. I''m using a fstream with a binary file. Initially what i wanted to do is to read a value from the file which was just written to it. So i''ll need a in|out mode.

What i thought so far is that the get pointer of the stream is used and advanced when performing a read() and accordingly the put pointer when using write() (i only worked with ifstream and ofstream before).

But as the following test shows, it seems that they are in fact allways pointing to the same position. So both the pointers are advanced when doing a read(), write or seek() operation.

std::fstream inOutStream("tmp_file", std::ios::out | std::ios::in | std::ios::binary | std::ios::ate);
    if(inOutStream)
    {
        double tmp = 123.123123;
        std::cout << "Put pointer position after opening stream: " << inOutStream.tellp() << std::endl;
        std::cout << "Get pointer position after opening stream: " << inOutStream.tellg() << std::endl;

        inOutStream.write((char*) &tmp, sizeof(double));
        inOutStream.flush();

        std::cout << "Put pointer position after writing to stream: " << inOutStream.tellp() << std::endl;
        std::cout << "Get pointer position after writing to stream: " << inOutStream.tellg() << std::endl;

        inOutStream.seekg(std::ios_base::beg);

        std::cout << "Put pointer position after moving get pointer to begin: " << inOutStream.tellp() << std::endl;
        std::cout << "Get pointer position after moving get pointer to begin: " << inOutStream.tellg() << std::endl;

        double tmp2 = 0.0;
        inOutStream.read((char*)&tmp2, sizeof(double));

        std::cout << "Put pointer position after reading from stream: " << inOutStream.tellp() << std::endl;
        std::cout << "Get pointer position after reading from stream: " << inOutStream.tellg() << std::endl;
}



输出为:



And the output is:

Put pointer position after opening stream: 0
Get pointer position after opening stream: 0
Put pointer position after writing to stream: 8
Get pointer position after writing to stream: 8
Put pointer position after moving get pointer to begin: 0
Get pointer position after moving get pointer to begin: 0
Put pointer position after reading from stream: 8
Get pointer position after reading from stream: 8



我的假设不是人们所期望的,如果指针指向不同位置会更方便吗?



Wasn''t my assumption something one would expect and wouldn''t it be more convenient if the pointers where to point at different positions?

推荐答案

不同,并且内部对seekpos(pos_type __pos, ios_base::openmode)的调用忽略了openmode参数. Microsoft VC和gcc的实现都忽略了它.我在fstream源中找到了gcc
的评论
The pointers are not different, and the internal call to seekpos(pos_type __pos, ios_base::openmode) is ignoring the openmode parameter. The implementation for Microsoft VC and gcc both ignore it. I found a comment in the fstream source for gcc
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 171. Strange seekpos() semantics due to joint position
// According to the resolution of DR 171, seekpos should ignore the last
// argument (of type openmode).

将我带到缺陷报告DR 171,请参见pdf 此处 [ ^ ],然后向下滑动至(或搜索)第35页的171. >
简而言之,它建议实现应查找发送到对open()的调用而不是对seekpos()的调用的openmode.因此,行为有些不直观.

错过了""

根据basic_filebuf规范:输入序列和输出序列都保持联合文件位置"

which lead me to the Defect Report, DR 171, see pdf here[^] and scoll down to (or seach for) 171 on page 35.

In short it suggests that implementations should look for the openmode sent to the call to open() instead of the call to seekpos(). Hence the somewhat unintuitive behavior.

missed a ''not''

According to the basic_filebuf specification: "A joint file position is maintained for both the input sequence and the output sequence"


这篇关于fstream的put和get指针之间有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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