- int VengConfig::GetFileType(LPCTSTR lpszFileName)
- {
- CFile file;
- byte buf[3]= {0};
- int tc;
- try
- {
- if(file.Open(lpszFileName,CFile::modeRead|CFile::shareDenyNone|CFile::typeBinary))
- {
- file.Read(buf,3);
- if(buf[0]==0xEF && buf[1]==0xBB && buf[2]==0xBF)
- tc=0;
- else if(buf[0]==0xFF && buf[1]==0xFE )
- tc=1 ;
- //else if(buf[0]==0xFE && buf[1]==0xFF )
- // tc=sUNICODEBIGENDIAN;
- else
- tc=3;
- }
- else
- tc=4;
- }
- catch (CFileException ex)
- {
- CString errormsg;
- errormsg.Format(_T("操作文件%s时发生异常!"),ex.m_strFileName);
- cout<<errormsg<<endl;
- }
- return tc;
- }
- string VengConfig::AllFileDataTypeConventToString(string filepath,int nType)
- {
- string FileData;
- FileData.empty();
- switch (nType)
- {
- case (3):
- {
- ifstream btfile(filepath.c_str(),ios::in|ios::binary);
- char buf[1024] = {0};
- ostringstream osTempFileData;
- osTempFileData<<btfile.rdbuf();
- FileData = osTempFileData.str();
- }
- break;
- case 1:
- {
- FILE *pfile = NULL;
- errno_t erCode = fopen_s(&pfile,filepath.c_str(), "rb");
- fseek(pfile, 2, 0);
- wchar_t buf[1024]={0};
- wstring line;
- while (!feof(pfile))
- {
- fgetws(buf, 1024, pfile);
- line += buf;
- }
- fclose(pfile);
- DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,line.c_str(),-1,NULL,0,NULL,FALSE);
- PBYTE TmpBuf = new BYTE[dwNum];
- WideCharToMultiByte (CP_OEMCP,NULL,line.c_str(),-1,(PCHAR)TmpBuf,dwNum,NULL,FALSE);
- FileData = (PCHAR)TmpBuf;
- delete TmpBuf;
- }
- break;
- case 0:
- {
- wchar_t buf[1024] = {0};
- FILE *fp = NULL;
- wstring line;
- _wfopen_s(&fp,CString(filepath.c_str()),_T("rt+,ccs=UTF-8"));
- locale loc("");
- wcout.imbue(loc);
- while(!feof(fp))
- {
- fgetws(buf,1024,fp);
- line += buf;
- }
- fclose(fp);
- DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,line.c_str(),-1,NULL,0,NULL,FALSE);
- PBYTE TmpBuf = new BYTE[dwNum];
- WideCharToMultiByte (CP_OEMCP,NULL,line.c_str(),-1,(PCHAR)TmpBuf,dwNum,NULL,FALSE);
- FileData = (PCHAR)TmpBuf;
- delete TmpBuf;
- }
- break;
- case 4:
- break;
- default:
- break;
- }
- return FileData;
- }
转载于:https://blog.51cto.com/venglu/1185227