zoukankan      html  css  js  c++  java
  • 使用不同编码读取文本

    string COfficeControlTestToolDlg::ReadFile(CString strFilePath)  

    {  

        CFile mFile;  

       if(!mFile.Open(strFilePath,CFile::modeRead|CFile::typeBinary))  

      {  

            MessageBox(_T("无法打开文件:")+strFilePath,_T("错误"),MB_ICONERROR|MB_OK);  

            PostQuitMessage(0);  

        }  

      

        m_isUnicode = FALSE;  

        m_isUTF_8Code = FALSE;  

      

        byte head[3];   //get head content  

        string strContents;   // file contents  

        UINT FileSize;    // file size  

        char *buf;        // temp   

        mFile.Read(head,3);    

        if((head[0]==0xff&&head[1]==0xfe)||(head[0]==0xfe&&head[1]==0xff))  //Test file Is Unicode ??  

        {  

            m_isUnicode = true;  

        }  

      

        if ((head[0]==0xef && head[1]==0xbb && head[2]==0xbf) || (head[0]==0xbf && head[1]==0xbb && head[2]==0xef))   //Test file Is UTF-8??  

        {  

            m_isUTF_8Code = true;  

        }  

      

        if (m_isUTF_8Code)  //read UTF-8 File  

        {  

      

            FileSize = (UINT)mFile.GetLength();  

            buf = new char[FileSize];  

            mFile.Seek(3,CFile::begin); //0xefbbbf  

            mFile.Read(buf,FileSize);  

            int size = MultiByteToWideChar(CP_UTF8,0,buf,FileSize+1,NULL,0);  

            wchar_t* pWideChar=new wchar_t[size+1];  

            MultiByteToWideChar(CP_UTF8,0,buf,FileSize+1,pWideChar,size);  

            strContents = CString(pWideChar).GetBuffer(0);  

            delete[] buf;  

            delete[] pWideChar;  

      

        }  

        else if(m_isUnicode)  //read Unicode File;  

        {  

            int i = 1;  

            wchar_t wch;       //for unicode  

            wchar_t wstr[300];  // for unicode  

            memset((void*)wstr, 0, sizeof(char)*(2*300));  

            mFile.Seek(2,CFile::begin); //0xfffe  

            while(mFile.Read((char *)&wch,2)>0)  

            {  

                if(wch==0x000D) //by line  

                {  

                    //change to ANSI  

                    int nLen = i;  

                    buf = new char[2*nLen];   

                    memset((void*)buf, 0, sizeof(char)*(2*nLen));  

                    WideCharToMultiByte(CP_ACP, 0, wstr, -1, buf, 2*nLen, NULL, NULL);  

                    buf[2*nLen-1] = '';   

                    strContents += buf;  

                    delete[] buf;  

                    i=0;  

                }  

                else  

                {  

                    wstr[i++] = wch;  

                }  

            }  

        }  

        else    //read ANSI file  

        {  

            FileSize = (UINT)mFile.GetLength();  

            buf = new char[FileSize];  

            while(mFile.Read(buf,FileSize)>0)  

            {  

                strContents = buf;  

            }  

            delete[] buf;  

        }  

        mFile.Close();  

        return strContents;  

    }  

  • 相关阅读:
    6_10 下落的树叶(UVa699)<二叉树的DFS>
    6_9 天平(UVa839)<二叉树的DFS>
    6_8 树(UVa548)<从中序和后序恢复二叉树>
    6_7 树的层次遍历(UVa122)<二叉树的动态创建与BFS>
    6_6 小球下落(UVa679)<完全二叉树编号>
    6_4 破损的键盘(UVa11988)<链表>
    6_3 矩阵链乘(UVa424)<用栈实现简单的表达式解析>
    6_2 铁轨(UVa514)<栈>
    第五周课程总结&试验报告(三)
    第四周课程总结和实验报告
  • 原文地址:https://www.cnblogs.com/whiteIcrow/p/8253786.html
Copyright © 2011-2022 走看看