zoukankan      html  css  js  c++  java
  • 非Unicode工程读取Unicode文件

    MyUnicodeReader.h

    #pragma once
    /************************************************************************/
    /* 在“多字节字符集”属性的工程中读取Unicode文件
    ** -----------------------------------注意-------------------------------------------------------
    ** -------------一定要确保读取的文件是标准的Unicode文件,即文件头两个字节是0xFFFE---------*/
    /************************************************************************/
    class MyUnicodeReader
    {
    public:
        MyUnicodeReader(void);
        ~MyUnicodeReader(void);
        FILE* file;
        bool Open(CString filePath);
        void Close();
        //按行读取
        bool ReadString(CString &s);
    };

    MyUnicodeReader.cpp

    #include "StdAfx.h"
    #include "MyUnicodeReader.h"
    #include <locale.h>
    #include <string>
    
    
    MyUnicodeReader::MyUnicodeReader(void)
    {
    }
    
    
    MyUnicodeReader::~MyUnicodeReader(void)
    {
    }
    
    bool MyUnicodeReader::Open( CString filePath )
    {
        file=fopen(filePath, "rb");
        //Unicode文件开始前两个字节应该是FFFE
        fseek(file, 2, SEEK_SET);
        return file!=NULL;
    }
    
    void MyUnicodeReader::Close()
    {
        fclose(file);
    }
    
    const int MAX_CHAR_NUM=1024;
    bool MyUnicodeReader::ReadString( CString &s )
    {
        wchar_t buf[MAX_CHAR_NUM];
        if (fgetws(buf, MAX_CHAR_NUM, file)==NULL) return false;
        size_t convertedChars = 0;
        char dst[MAX_CHAR_NUM];
        setlocale(LC_CTYPE,"chs");// 处理汉字
        wcstombs_s(&convertedChars, dst, MAX_CHAR_NUM, buf, MAX_CHAR_NUM);
        s=dst;
        return true;
    }
  • 相关阅读:
    常见概念
    网站扒取
    图书管理系统易错点整理
    CSIC_716_2020104【Django入门---静态文件、request对象方法、ORM增删改查】
    CSIC_716_2020103【Django入门---HttpResponse、render、redirect】
    CSIC_716_2020102【前端框架Bootstrap】
    CSIC_716_2020101【???】
    CSIC_716_20191231【jQuery基础入门】
    CSIC_716_20191230【前端的BOM与DOM】
    ss
  • 原文地址:https://www.cnblogs.com/coolbear/p/4758127.html
Copyright © 2011-2022 走看看