std::string filepath("C:\\");
std::ofstream file(filepath.c_str());
std::wstring filepath= L"C:\"
std::ofstream file(filepath.c_str());
- “wacky gcc” or “wacky stlport” does not contain the constructor ofstream :: ofstream (wchar_t *)
- #include <boost / filesystem / fstream.hpp>
- #include <string>
- namespace fs = boost :: filesystem ;
- int main ( int argc, char * argv )
- {
- std :: wstring filepath ( L "C: \ test" ) ;
- fs :: ofstream ( filepath ) ;
- return 0 ;
- }
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <locale>
- #include <memory>
- #include "facet / codecvt / codecvt_cp866.hpp"
- / ** @ brief Narrows a wide string using loc localization
- @return Returns a narrowed string or an empty narrowed string, in
- case. if an error occurs * /
- std :: string narrow ( const std :: wstring & wstr, const std :: locale & loc )
- {
- const size_t sz = wstr. length ( ) ;
- if ( sz == 0 )
- return std :: string ( ) ;
- mbstate_t state = 0 ;
- char * cnext ;
- const wchar_t * wnext ;
- const wchar_t * wcstr = wstr. c_str ( ) ;
- char * buffer = new char [ sz + 1 ] ;
- std :: uninitialized_fill ( buffer, buffer + sz + 1 , 0 ) ;
- typedef std :: codecvt < wchar_t , char , mbstate_t > cvt ;
- cvt :: result res ;
- res = std :: use_facet < cvt > ( loc ) . out ( state, wcstr, wcstr + sz, wnext,
- buffer, buffer + sz, cnext ) ;
- std :: string result ( buffer ) ;
- if ( res == cvt :: error )
- return std :: string ( ) ;
- return result ;
- }
- / ** @ brief Extends a string using loc localization
- @return Returns an extended string or an empty extended string, in
- if an error occurred. * /
- std :: wstring widen ( const std :: string & str, const std :: locale & loc )
- {
- const size_t sz = str. length ( ) ;
- if ( sz == 0 )
- return std :: wstring ( ) ;
- mbstate_t state = 0 ;
- const char * cnext ;
- wchar_t * wnext ;
- const char * cstr = str. c_str ( ) ;
- wchar_t * buffer = new wchar_t [ sz + 1 ] ;
- std :: uninitialized_fill ( buffer, buffer + sz + 1 , 0 ) ;
- typedef std :: codecvt < wchar_t , char , mbstate_t > cvt ;
- cvt :: result res ;
- res = std :: use_facet < cvt > ( loc ) . in ( state, cstr, cstr + sz, cnext,
- buffer, buffer + sz, wnext ) ;
- std :: wstring result ( buffer ) ;
- delete [ ] buffer ;
- if ( res == cvt :: error )
- return std :: wstring ( ) ;
- return result ;
- }
- int main ( int argc, char * argv [ ] )
- {
- // Let there be a cp866 file with a path
- std :: ofstream ofile ( "input.txt" , std :: ios :: binary ) ;
- if ( ! ofile )
- {
- std :: cerr << "Error open file" << std :: endl ;
- return 0 ;
- }
- std :: ostreambuf_iterator < char > writer ( ofile ) ;
- * ( writer ) = 0xe2 ; // t
- * ( ++ writer ) = 0xa5 ; // e
- * ( ++ writer ) = 0xe1 ; // with
- * ( ++ writer ) = 0xe2 ; // t
- ofile. close ( ) ;
- // Read the path
- std :: locale cp866 ( std :: locale ( ) , new codecvt_cp866 ) ;
- std :: wifstream ifile ( "input.txt" , std :: ios :: binary ) ;
- ifile. imbue ( cp866 ) ;
- std :: wstring wpath ;
- ifile >> wpath ;
- ifile >> wpath ;
- ifile. close ( ) ;
- // Create a file in this path
- std :: ofstream file ( narrow ( wpath, std :: locale ( "" ) ) . c_str ( ) ) ;
- file << "testing" ;
- file . close ( ) ;
- }
Source: https://habr.com/ru/post/112997/
All Articles