std::ifstream::readsome和std::ifstream::read的区别

std::ifstream::readsome的原型如下,可以返回实际读到的字节数量,但是不会把eofbit置1,所以不能直接调用eof判断是否到文件尾

streamsize readsome (char* s, streamsize n);

std::ifstream::read原型如下,会置eof, 但没法直接得到实际读取字节数

istream& read (char* s, streamsize n);

如果既需要能获得读取字节数,又需要能判断是否读到文件尾,可有如下两种方法:

1. 使用std::ifstream::readsome

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

streamsize Read(istream &stream, char *buffer, streamsize count)
{
    // This consistently fails on gcc (linux) 4.8.1 with failbit set on read
    // failure. This apparently never fails on VS2010 and VS2013 (Windows 7)
    streamsize reads = stream.rdbuf()->sgetn(buffer, count);

    // This rarely sets failbit on VS2010 and VS2013 (Windows 7) on read
    // failure of the previous sgetn()
    stream.rdstate();

    // On gcc (linux) 4.8.1 and VS2010/VS2013 (Windows 7) this consistently
    // sets eofbit when stream is EOF for the conseguences  of sgetn(). It
    // should also throw if exceptions are set, or return on the contrary,
    // and previous rdstate() restored a failbit on Windows. On Windows most
    // of the times it sets eofbit even on real read failure
    stream.peek();

    return reads;
}

int main(int argc, char *argv[])
{
    ifstream instream("filepath", ios_base::in | ios_base::binary);
    while (!instream.eof())
    {
        char buffer[0x4000];
        size_t read = Read(instream, buffer, sizeof(buffer));
        // Do something with buffer 
    }
}

2. 使用std::ifstream::read后,使用gcount()获得上一次读取的字节数

ifstream ifs("a.txt");
char buf[1024];
ifs.read(buf, 1024);
size_t extracted = ifs.gcount();

// or

ifstream ifs("a.txt");
char buf[1024];
size_t extracted = ifs.read(buf, 1024).gcount();

 

Ref:

https://stackoverflow.com/questions/9191876/when-does-ifstreamreadsome-set-eofbit

https://stackoverflow.com/questions/11720880/ifstreamread-doesnt-tell-how-many-bytes-it-really-reads

 

  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
引用\[1\]中的代码展示了如何使用std::ifstream来读取文件数据。首先,需要包含<iostream>和<fstream>头文件,并使用命名空间std。然后,在main函数中声明一个字符数组buffer来存储读取的数据。接下来,使用ifstream对象in打开名为"test.txt"的文件。如果文件打开失败,则输出错误信息并退出程序。然后,使用while循环和in.getline函数逐行读取文件内容,并将每行数据输出到控制台。最后,关闭文件并返回0表示程序执行成功。 引用\[2\]中介绍了fstream类及其派生类std::ofstream和std::ifstream的用法。其中,std::ofstream用于写文件操作,std::ifstream用于读文件操作。可以使用构造函数来初始化ifstream对象,其中第一个参数是文件名,第二个参数是打开文件的模式。 引用\[3\]中的代码展示了如何使用std::ofstream来写入文件数据。首先,使用ofstream对象out打开名为"test.txt"的文件。如果文件打开失败,则返回false。否则,使用out对象的插入操作符<<将数据写入文件中。最后,关闭文件并返回0表示程序执行成功。 综上所述,c++ std::ifstream.read函数可以用于从文件中读取数据。您可以使用ifstream对象的read函数来读取指定数量的字节,并将其存储在指定的缓冲区中。 #### 引用[.reference_title] - *1* *3* [C++文件操作](https://blog.csdn.net/king13059595870/article/details/103063316)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [C++ 文件读写操作std::ofstream和std::ifstream](https://blog.csdn.net/block999123/article/details/121869208)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值