Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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++ 使用wregex创建垃圾输出?_C++_Regex_Visual C++_C++11_Wofstream - Fatal编程技术网

C++ 使用wregex创建垃圾输出?

C++ 使用wregex创建垃圾输出?,c++,regex,visual-c++,c++11,wofstream,C++,Regex,Visual C++,C++11,Wofstream,我使用regex创建了一个简单的程序来标记文件。对于非统一代码内容,它可以正常工作。对于基于Unicode的内容,我制作了一个wregex版本,但这个版本会创建垃圾输出 我试图在控制台屏幕上输出Unicode字符或字符串,而不是将它们存储在映射和wostream类型的文件中,以便值保持完整和正确。 运行应用程序后,包含提取令牌的文件只包含垃圾 这个程序有什么问题,我如何修复它 #include "stdafx.h" #include <iostream> #include <

我使用regex创建了一个简单的程序来标记文件。对于非统一代码内容,它可以正常工作。对于基于Unicode的内容,我制作了一个wregex版本,但这个版本会创建垃圾输出

我试图在控制台屏幕上输出Unicode字符或字符串,而不是将它们存储在
映射和wostream类型的文件中,以便值保持完整和正确。
运行应用程序后,包含提取令牌的文件只包含垃圾

这个程序有什么问题,我如何修复它

#include "stdafx.h"

#include <iostream>
#include <regex>
#include <fstream>
#include <string>
#include <map>
using namespace std;

int main()
{
    string path="";    

    map<wstring, int> container;
    wifstream file("ftest.txt"); 
    wregex reg(_T("\\w+"));
    wstring s=_T("");
    while (file.good())
    {
        file>>s;
        for ( wsregex_iterator it (s.begin(), s.end(), reg),it_end; it != it_end; ++it)
        {
            container[(wstring)(*it)[0]]++ ;
        }

    }

    cout <<"\nDone..."<< endl;
    wofstream output("list.txt",ios::app);
    for (auto item : container)
    {
        //cout<<item.first<<" : "<<item.second<<endl;
        output<<item.first<<" : "<<item.second<<endl;
    }
    system("pause");
    return 0;
}
这是
list.txt中的垃圾输出

0 : 3
1 : 1
14 : 1
16 : 1
26 : 1
27 : 1
5 : 2
50 : 1
6 : 1
7 : 1
ط : 475
طھ : 12
طھط : 20
طھطµظ : 1
طھظ : 10
طھغ : 2
ط² : 6
ط²ط : 6
ط²ظ : 6
ط³ : 5
ط³ط : 12
ط³طھ : 8
ط³طھط : 4
ط³طھظ : 2
ط³ظ : 10
ط³غ : 1
طµ : 1
طµط : 1
طµظ : 6
ط¹ط : 1
ط¹ظ : 8
ظ : 291
ع : 54
غ : 95
ï : 1

您需要将文件的UTF8编码转换为
std::wregex
使用的UTF16编码

使用C++11,您可以使用:

这解决了我的问题。:)要获得便携式解决方案,请查看此

这是最终的代码,它可以完美地工作:):

#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括
#包括//for wfopen\s
#包括//for _setmode
使用名称空间std;
int main()
{
字符串路径=”;
地图容器;
文件*fp;
_wfopen_s(&fp,L“ftest.txt”,L“r”);
_setmode(_fileno(fp),_O_u8文本);
wifstream文件(fp);
wregex注册表(L“\\w+”);
wstring s=L”“;
while(file.good())
{
getline(文件,s);
对于(wsregex_迭代器it(s.begin(),s.end(),reg),it_end;it!=it_end;++it)
{
容器[(wstring)(*it)[0]]++;
}
}

你能确定你的文本文件是UTF16吗?而不是UTF8吗?它是UTF8!我现在该怎么办?要么你将文件转换为UTF16,要么你找到另一个支持UTF8字符串的正则表达式库。我得到以下错误:错误1错误C2061:语法错误:标识符“codecvt\u UTF8\u UTF16”错误2错误C2665:“std::locale::locale”:9个重载中没有一个可以转换所有参数类型3 IntelliSense:构造函数“std::locale::locale”的实例不匹配参数列表参数类型为:(std::locale,)4 IntelliSense:应为类型说明符5 IntelliSense:不允许使用类型名称6 IntelliSense:应为表达式我正在使用Visual Studio 2012 SP3
0 : 3
1 : 1
14 : 1
16 : 1
26 : 1
27 : 1
5 : 2
50 : 1
6 : 1
7 : 1
ط : 475
طھ : 12
طھط : 20
طھطµظ : 1
طھظ : 10
طھغ : 2
ط² : 6
ط²ط : 6
ط²ظ : 6
ط³ : 5
ط³ط : 12
ط³طھ : 8
ط³طھط : 4
ط³طھظ : 2
ط³ظ : 10
ط³غ : 1
طµ : 1
طµط : 1
طµظ : 6
ط¹ط : 1
ط¹ظ : 8
ظ : 291
ع : 54
غ : 95
ï : 1
std::wifstream file("ftest.txt"); 
file.imbue(std::locale(file.getloc(), new std::codecvt_utf8_utf16<wchar_t>());
// "file" will now read UTF8 and output UTF16.
auto w_s = boost::locale::utf_to_utf<char>(s);
#include "stdafx.h"
#include <iostream>
#include <regex>
#include <fstream>
#include <string>
#include <map>
#include <fcntl.h> // for _wfopen_s
#include <io.h> //for _setmode


using namespace std;

int main()
{
    string path = "";    

    map<wstring, int> container;

     FILE* fp;
    _wfopen_s (&fp, L"ftest.txt", L"r");
    _setmode (_fileno (fp), _O_U8TEXT);

    wifstream file(fp);
    wregex reg(L"\\w+");

    wstring s = L"";

    while (file.good())
    {
        getline(file,s);    
        for ( wsregex_iterator it (s.begin(), s.end(), reg), it_end ; it != it_end ; ++it)
        {
            container[(wstring)(*it)[0]]++ ;
        }
    }

    cout <<"\nDone..."<< endl;

    fclose(fp);

    _wfopen_s (&fp, L"list.txt", L"w");
    _setmode (_fileno (fp), _O_U8TEXT);
    wofstream output(fp);

    for (auto item : container)
    {
        wcout<<item.first <<" : "<<item.second <<endl;
        //write output to list.txt
        output<<item.first <<" : "<<item.second <<endl;
    }
    fclose(fp);
    system("pause");
    return 0;
}