如何在 Win32 上的缓冲区中读取整个文件wchar_t
How do I read the whole file in wchar_t buffer on Win32?
我在将 UTF8 编码的文件读取到wchar_t
缓冲区时遇到问题,因为我事先不知道文件大小。
有谁知道我如何在缓冲区中读取整个文件?
我想我必须保留一个wchar_t *
(指针)并在阅读时调整它的大小。然而,这听起来很可怕,因为我以前从未调整过指针的大小。
我正在使用Microsoft Visual Studio进行Windows C++编程。
您是否考虑过使用向量?
#include <vector>
#include <fstream>
#include <iterator>
:::
std::wifstream in(file_name);
//Will automatically reallocate to the require size
std::vector<wchar_t> store {
std::istream_iterator<wchar_t, wchar_t>(in),
std::istream_iterator<wchar_t, wchar_t>()};
//to access wchar_t* you can call data();
my_func(store.data());
如果要
分配文件大小的缓冲区,则首先需要查找文件大小。 为此,您可以使用适当的参数调用 stat() 函数,它会填充包含文件大小的字段。
假设您将该大小存储在变量 filesize
中。
然后你可以
wchar_t *buffer = reinterpret_cast< wchar_t * >new char [ filesize ];
然后在调用 open() 获取文件描述符后,使用 read() 将整个文件读入该缓冲区,或者在调用 fopen() 获取文件 * 后使用 fread() 读取整个文件。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- Cppcheck生成xml转储文件
- 【MacOSX 10.15.1】使用 odb 生成数据库,致命错误:wchar.h:没有这样的文件或目录 #include < wchar.h>
- 找不到WCHAR.H文件-Xcode 9.0中断QT创建者 - 旧修复程序不起作用
- 恢复 wchar.h c++ 包含文件
- 使用文件路径填充WCHAR阵列