读取UNICODE文本到wstring

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <locale>

using namespace std;

void read_memo()
{
    FILE* pfile = fopen("D:\\memo.txt", "rb");
    if (!pfile) return;
    fseek(pfile, 2, 0);

    wchar_t buf[1024];
    wstring line;
    const wstring CRLF = L"\x000D\x000A";

    while (!feof(pfile))
    {
        fgetws(buf, 1024, pfile); 
        line = buf;

        //Trim the CRLF(ENTER+RETURN) character
        if(line.find(CRLF) != wstring::npos)
            line = line.substr(0, line.find(CRLF));
    }

    fclose(pfile);
}


Unicode环境下使用pugixml读取节点下的属性文本,可以按照以下步骤进行: 1. 首先,使用`pugi::xml_document`类加载XML文件。例如,如果要加载名为"example.xml"的XML文件,可以使用以下代码: ``` pugi::xml_document doc; doc.load_file("example.xml"); ``` 2. 然后,使用`pugi::xml_node`类获取要读取属性的节点。例如,如果要读取名为"node1"的节点下的"attribute1"属性,可以使用以下代码: ``` pugi::xml_node node = doc.child("root").child("node1"); std::string attribute_value = node.attribute("attribute1").value(); ``` 这里假设XML文件的根节点为"root"。 3. 最后,使用`std::wstring_convert`类将属性值从UTF-8编码转换为Unicode字符串。例如,如果要将属性值转换为UTF-16编码的Unicode字符串,可以使用以下代码: ``` std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; std::wstring unicode_value = converter.from_bytes(attribute_value); ``` 现在,变量`unicode_value`包含了UTF-16编码的属性值,可以在Unicode环境下使用它了。 完整的示例代码如下: ``` #include <pugixml.hpp> #include <locale> #include <codecvt> #include <string> int main() { // Load XML file pugi::xml_document doc; doc.load_file("example.xml"); // Get attribute value pugi::xml_node node = doc.child("root").child("node1"); std::string attribute_value = node.attribute("attribute1").value(); // Convert attribute value to Unicode std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; std::wstring unicode_value = converter.from_bytes(attribute_value); // Use Unicode value // ... return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值