Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.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++ C++;使用UCS-2 LE编码在文件中搜索短语_C++_Character Encoding_Ucs2 - Fatal编程技术网

C++ C++;使用UCS-2 LE编码在文件中搜索短语

C++ C++;使用UCS-2 LE编码在文件中搜索短语,c++,character-encoding,ucs2,C++,Character Encoding,Ucs2,我需要写一个程序来搜索一个文本文件,并列出这个文件包含的名字。每个名字都放在“[PRG]”短语后面。所以我想搜索“PRG”,然后读下一个单词。但我有一个问题,因为这个文件的编码是UCS-2LE。我发现我需要使用“wstring”变量而不是“string”。但我看到我从文件中读取奇怪的值,我无法将它们与“PRG”短语进行比较 代码如下: int main() { wstring textBuff; // Buffor for reading text from a file wstring sea

我需要写一个程序来搜索一个文本文件,并列出这个文件包含的名字。每个名字都放在“[PRG]”短语后面。所以我想搜索“PRG”,然后读下一个单词。但我有一个问题,因为这个文件的编码是UCS-2LE。我发现我需要使用“wstring”变量而不是“string”。但我看到我从文件中读取奇怪的值,我无法将它们与“PRG”短语进行比较

代码如下:

int main() {
wstring textBuff; // Buffor for reading text from a file
wstring searchBuff = L"PRG"; // Variable containing searching phrase

wifstream file;
file.open("programs.prg", ios::in | ios::binary);

if (file.good()) {
    // Reading file and listing every word after "PRG" phrase
    while (!file.eof()) {
        file >> textBuff;
        if (textBuff.find(searchBuff) != string::npos)
            wcout << textBuff << endl;
    }
}
file.close();
system("pause");
return 0;
}
intmain(){
wstring textBuff;//用于从文件中读取文本的buffer
wstring searchBuff=L“PRG”;//包含搜索短语的变量
wifstream文件;
打开(“programs.prg”,ios::in | ios::binary);
if(file.good()){
//读取文件并在“PRG”短语后列出每个单词
而(!file.eof()){
文件>>textBuff;
if(textBuff.find(searchBuff)!=string::npos)

wcout如果看不到文件中的数据副本或一些非常相似的数据,就很难提供太多帮助

出现了许多问题,我怀疑其中任何一个问题都很重要,因为您称自己为“编程新手”

从阅读开始做一些研究

特别是标题为: 字节顺序编码方案

如果您的数据在“PRG”标签后有一个BOM表,那么您必须通过编码适当的尾数来处理它

请阅读以下内容:

尤其是底部的音符


t博士

谢谢你的回答。我将该文件添加到我的主要帖子中。调试完程序后,我看到第一个值是“'255;”、“þ”、“['、'\0'、't'\0'、'Y'、'\0'、'p'、'\0']'”而不是“[TYP]”.我想前两个字符一定是BOM,但我真的不知道我能做些什么。有没有办法去掉它们?我只需要“查找”功能就行了。这是这个程序唯一不起作用的部分。