- wio
-
#include <assert.h>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
const int FBLOCK_MAX_BYTES = 256; // File Type.
typedef enum FileType
{
ANSI = 0,
UNICODE,
UTF8,
} FILETYPE;
FILETYPE GetTextFileType(const std::string & strFileName);
int UnicodeToANSI(char * pDes, const wchar_t * pSrc);
void main() { // file test.
std::string strFileANSI = "C://Hello_ANSI.txt";
std::string strFileUNICODE = "C://Hello_UNICODE.txt";
std::string strFileUTF8 = "C://Hello_UTF8.txt"; // please change the file name to test.
std::string strFileName = strFileUTF8;
FILETYPE fileType = GetTextFileType(strFileName);
if (UNICODE == fileType)
{
wchar_t szBuf[FBLOCK_MAX_BYTES];
memset(szBuf, 0, sizeof(wchar_t) * FBLOCK_MAX_BYTES);
std::string strMessage;
FILE * fp = NULL;
fp = fopen(strFileName.c_str(), "rb");
if (fp != NULL)
{ // Unicode file should offset wchar_t bits(2 byte) from start.
fseek(fp, sizeof(wchar_t), 0);
while(fread(szBuf, sizeof(wchar_t), FBLOCK_MAX_BYTES, fp) > 0)
{
char szTemp[FBLOCK_MAX_BYTES] = {0};
UnicodeToANSI(szTemp, szBuf);
strMessage += szTemp;
memset(szBuf, 0, sizeof(wchar_t) * FBLOCK_MAX_BYTES);
}
}
std::cout << strMessage << std::endl;
fclose(fp);
}
else
if (UTF8 == fileType)
{
char szBuf[FBLOCK_MAX_BYTES];
memset(szBuf, 0, sizeof(char) * FBLOCK_MAX_BYTES);
std::string strMessage;
FILE * fp = NULL;
fp = fopen(strFileName.c_str(), "rb");
if (fp != NULL)
{ // UTF-8 file should offset 3 byte from start position.
fseek(fp, sizeof(char) * 3, 0);
while(fread(szBuf, sizeof(char), FBLOCK_MAX_BYTES, fp) > 0)
{
strMessage += szBuf;
memset(szBuf, 0, sizeof(char) * FBLOCK_MAX_BYTES);
}
}
std::cout << strMessage << std::endl;
fclose(fp);
}
else
{
char szBuf[FBLOCK_MAX_BYTES];
memset(szBuf, 0, sizeof(char) * FBLOCK_MAX_BYTES);
std::string strMessage;
FILE * fp = NULL;
fp = fopen(strFileName.c_str(), "rb");
if (fp != NULL)
{
// common file do not offset.
while(fread(szBuf, sizeof(char), FBLOCK_MAX_BYTES, fp) > 0)
{
strMessage += szBuf;
memset(szBuf, 0, sizeof(char) * FBLOCK_MAX_BYTES);
}
}
std::cout << strMessage << std::endl;
fclose(fp);
}
#ifdef _DEBUG
getchar();
#endif
}
FILETYPE GetTextFileType(const std::string & strFileName)
{
FILETYPE fileType = ANSI;
std::ifstream file;
file.open(strFileName.c_str(), std::ios_base::in);
bool bUnicodeFile = false;
if (file.good())
{
char szFlag[3] = {0};
file.read(szFlag, sizeof(char) * 3);
if ((unsigned char)szFlag[0] == 0xFF&& (unsigned char)szFlag[1] == 0xFE)
{
fileType = UNICODE;
}
else
if ((unsigned char)szFlag[0] == 0xEF && (unsigned char)szFlag[1] == 0xBB && (unsigned char)szFlag[2] == 0xBF)
{
fileType = UTF8;
}
}
file.close();
return fileType;
}
int UnicodeToANSI(char * pDes, const wchar_t * pSrc)
{
assert(pDes != NULL);
assert(pSrc != NULL);
int nLen = ::WideCharToMultiByte(CP_ACP, 0, pSrc, -1, NULL, 0, NULL, NULL);
if (nLen == 0)
{
return -1;
}
return ::WideCharToMultiByte(CP_ACP, 0, pSrc, -1, pDes, nLen, NULL, NULL);
}