Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用wstring获取行读取文件_C++_String_Ifstream_Wstring - Fatal编程技术网

C++ 使用wstring获取行读取文件

C++ 使用wstring获取行读取文件,c++,string,ifstream,wstring,C++,String,Ifstream,Wstring,我有一个包含文本的文件,我想将此文件中的每一行都放入一个std::wstring变量。如果这样做,我会得到一个错误,那么是否可以使用std::wstring或者我必须使用std::string?这是我的代码: std::ifstream fichier( "text.txt" ); if ( fichier ) { std::wstring ligne; while ( std::getline( fichier, ligne ) ) { //work with li

我有一个包含文本的文件,我想将此文件中的每一行都放入一个
std::wstring
变量。如果这样做,我会得到一个错误,那么是否可以使用
std::wstring
或者我必须使用
std::string
?这是我的代码:

std::ifstream fichier( "text.txt" );

if ( fichier )
{
  std::wstring ligne;

  while ( std::getline( fichier, ligne ) )
  {
      //work with ligne
  }
}

如前所述,如果我用
std::string
替换
std::wstring
,我没有错误。

尝试使用
std::wifstream
而不是
std::ifstream

来使用wstring读取文件,我发现
wifstream
效果非常好,即使在visual studio调试器中正确显示UTF-8字(我在读繁体中文),来自:

#包括
#包括
#包括
std::wstring读取文件(常量字符*文件名)
{
std::wifstream wif(文件名);
imbue(std::locale(std::locale::empty(),新std::codevt_utf8));
std::wstringstream wss;
wss
#include <sstream>
#include <fstream>
#include <codecvt>

std::wstring readFile(const char* filename)
{
    std::wifstream wif(filename);
    wif.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t>));
    std::wstringstream wss;
    wss << wif.rdbuf();
    return wss.str();
}

//  usage
std::wstring wstr2;
wstr2 = readFile("C:\\yourUtf8File.txt");
wcout << wstr2;