Load a text file with C++
less than 1 minute read
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
std ::wstring fromAscii ( const char * str )
{
std ::wstring sOutput ;
int inputLength = ( int ) strlen ( str ) ;
if ( inputLength )
{
inputLength ++; // allow for null terminator
wchar _ t * buf = new wchar _ t [ inputLength ] ;
MultiByteToWideChar ( CP _ ACP , 0 , str , inputLength , buf , inputLength ) ;
sOutput += buf ;
delete [] buf ;
}
return sOutput ;
}
bool LoadTextFile ( std ::wstring const & path , std ::wstring & text )
{
try
{
std ::ifstream ifs ( path ) ;
std ::string str (( std ::istreambuf _ iterator ( ifs )) , std ::istreambuf _ iterator ()) ;
text = fromAscii ( str . c _ str ()) ;
return true ;
}
catch ( ... )
{
}
return false ;
}