C ++读取缓冲区中的整个文件

编程入门 行业动态 更新时间:2024-10-27 14:21:09
本文介绍了C ++读取缓冲区中的整个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

什么是读取C ++缓冲区中整个文件内容的好方法?

What is a good approach to read the whole file content in a buffer for C++?

在普通CI中,可以使用 fopen() ,fseek(),fread()函数组合并将整个文件读取到缓冲区中,对于C ++使用相同的文件还是个好主意吗?如果是,那么我如何在打开时使用RAII方法,为缓冲区分配内存,将文件内容读取和读取到缓冲区中。

While in plain C I could use fopen(), fseek(), fread() function combination and read the whole file to a buffer, is it still a good idea to use the same for C++? If yes, then how could I use RAII approach while opening, allocating memory for buffer, reading and reading file content to buffer.

我应该为缓冲区创建一些包装器类,该类在其析构函数中分配内存(为缓冲区分配),以及用于文件处理的相同包装器吗?

Should I create some wrapper class for the buffer, which deallocates memory (allocated for buffer) in it's destructor, and the same wrapper for file handling?

推荐答案

对于非常基本的功能,不需要包装类:

There's no need for wrapper classes for very basic functionality:

std::ifstream file("myfile", std::ios::binary | std::ios::ate); std::streamsize size = file.tellg(); file.seekg(0, std::ios::beg); std::vector<char> buffer(size); if (file.read(buffer.data(), size)) { /* worked! */ }

更多推荐

C ++读取缓冲区中的整个文件

本文发布于:2023-07-27 02:44:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219897.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区中   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!