CやC++でワイド文字列パスのファイルを開く
0.CやC++でワイド文字列パスのファイルを開き、バイナリ読み書きしたい。
1._wfopenを使用する。
Download Visual Studio 2005 Retired documentation from Official Microsoft Download Center
#include <cstdio> #include <vector> bool open_with_wfopen(wchar_t const *filepath) { FILE *fp = _wfopen(filepath, L"rb"); if(!fp) { return false; } std::vector<char> tmp(256); fread(&tmp[0], sizeof(char), tmp.size() - 1, fp); std::cout << &tmp[0] << "\n\n" << std::endl; fclose(fp); return true; }
2.ファイルハンドルを使用する。
c - Is there a Windows equivalent to fdopen for HANDLEs? - Stack Overflow
Windows previous versions documentation | Microsoft Docs
Download Visual Studio 2005 Retired documentation from Official Microsoft Download Center
Download Visual Studio 2005 Retired documentation from Official Microsoft Download Center
#include <io.h> #include <fcntl.h> #include <cstdio> #include <vector> #include <windows.h> bool open_with_handle_to_fopen(wchar_t const *filepath) { HANDLE file_handle = CreateFileW( filepath, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_RANDOM_ACCESS, 0 ); if(file_handle == INVALID_HANDLE_VALUE) { return false; } int crt_file_handle = _open_osfhandle(reinterpret_cast<intptr_t>(file_handle), _O_RDONLY|_O_BINARY); if(crt_file_handle == -1) { CloseHandle(file_handle); return false; } file_handle = NULL; //now use _close() instead CloseHandle() FILE *fp = _fdopen(crt_file_handle, "rb"); if(!fp) { _close(crt_file_handle); return false; } crt_file_handle = -1; //now use fclose() instead _close() std::vector<char> tmp(256); fread(&tmp[0], sizeof(char), tmp.size() - 1, fp); std::cout << &tmp[0] << "\n\n" << std::endl; fclose(fp); return true; }
3.fstreamを使用する。
ifstream::ifstream - C++ Reference
c++ - How to open an std::fstream (ofstream or ifstream) with a unicode filename? - Stack Overflow
#include <fstream> #include <vector> bool open_with_fstream(wchar_t const *filepath) { std::ifstream is(filepath, std::ios_base::in|std::ios_base::binary); if(!is) { return false; } std::vector<char> tmp(256); is.read(&tmp[0], sizeof(char) * (tmp.size()-1)); std::cout << &tmp[0] << "\n\n" << std::endl; return true; }
4. Boost.Iostreamsを使用する。
#include <vector> #include <boost/iostreams/stream.hpp> #include <boost/iostreams/device/file_descriptor.hpp> bool open_with_boost_iostream(wchar_t const *filepath) { namespace ios = boost::iostreams; namespace fs = boost::filesystem; ios::stream<ios::file_descriptor_source> is(fs::path(filepath), std::ios_base::in|std::ios_base::binary); if(!is) { return false; } std::vector<char> tmp(256); is.read(&tmp[0], sizeof(char) * (tmp.size()-1)); std::cout << &tmp[0] << "\n\n" << std::endl; return true; }
5.Boost.Filesystemを使用する。
#include <vector> #include <boost/filesystem/fstream.hpp> bool open_with_boost_filesystem(wchar_t const *filepath) { namespace fs = boost::filesystem; fs::path const bpath = filepath; fs::ifstream is(bpath); if(!is) { return false; } std::vector<char> tmp(256); is.read(&tmp[0], sizeof(char) * (tmp.size()-1) ); std::cout << &tmp[0] << "\n\n" << std::endl; return true; }