64,683
社区成员
发帖
与我相关
我的任务
分享
std::vector<std::wstring>ReadAllLinesByUnicode(std::wstring filePath)
{
std::vector<std::wstring> list;
FILE *fp = NULL;
_wfopen_s(&fp,filePath.c_str() , L"rb,ccs=Unicode");
wchar_t buf[MAXLINESIZE+1] = {0}; // 假设每一行文本不超过2048字符
size_t rdCount = fread(buf, 1, 2, fp);
if (rdCount != 2)
{
fclose(fp);
return list;
}
if (buf[0] != 0xFF || buf[1] != 0xFE)
{
fseek(fp, 0, SEEK_SET); // 测试Unicode文件标志, 没有Unicode头,跳回文件头
}
while (!feof(fp))
{
if (NULL != fgetws(buf, MAXLINESIZE, fp))
{
list.push_back(buf);
}
}
fclose(fp);
return list;
}
wstring path=L"./data/input.txt";
wchar_t * str =L"中国";
vector<wstring> list = ReadAllLinesByUnicode(path);
for(int i=0;i<list.size();i++)
{
//int tmp = dic->InsertEntry(list[i].c_str());
if(wcscmp(list[i].c_str(), str)==0)
{
cout<<"OK\n";
}
}
_wfopen_s(&fp,filePath.c_str() , L"r,ccs=Unicode"); //不用二进制读?
wchar_t buf[MAXLINESIZE+1] = {0}; // 假设每一行文本不超过2048字符
size_t rdCount = fread(buf, 1, 2, fp);
if (rdCount != 2)
{
fclose(fp);
return list;
}
if (buf[0] != 0xFF || buf[1] != 0xFE)
{
fseek(fp, 0, SEEK_SET); // 测试Unicode文件标志, 没有Unicode头,跳回文件头
}
fseek(fp, 2, SEEK_SET); //unicode好像会有个文件头
while (!feof(fp))
{
if (NULL != fgetws(buf, MAXLINESIZE, fp))
{
char* t = (char*)buf;
int len = strlen(t);
t[len-1]=0;
list.push_back(buf);
}
}