MFC----文件操作

GetDlgItemText(pInfo->hDbgDlg, IDC_EDIT_CONFIG_FILE, swBuf, MAX_PATH);
LogOut(LEVEL_MSG, swBuf);

ReadBinFile(swBuf, outData, 256);  //read data from bin file


int ReadBinFile(TCHAR    *pfilename, CHAR *pData, ULONGLONG fileSize)
{
    INT            iRet = 0;
    CFile        BinFile;
    CString        strFilename;
    ULONGLONG    TotalFileSize;

    CString        strTemp;
    TCHAR        cTemp[MAX_TEMP];

    strFilename.Format(L"%s", pfilename);

    if (!BinFile.Open(strFilename, CFile::modeRead))
    {
        strTemp.Format(_T("Failed to open file %s"), strFilename);
        _tcscpy(cTemp, strTemp);
        LogOut(LEVEL_MSG, cTemp);
    }

    TotalFileSize = BinFile.GetLength();
    if (fileSize > TotalFileSize)
    {
        strTemp = _T("The read data size is over the total size");
        _tcscpy(cTemp, strTemp);
        LogOut(LEVEL_MSG, cTemp);
    }

    BinFile.Read(pData, fileSize);

    BinFile.Close();

    return iRet;
}
int ReadBinFile(TCHAR	*ptcFileName, UCHAR *pData, ULONGLONG fileSize)
{
	INT			iRet = 0;
	FILE		*pReadFile;
	char 		*pcFileName;
	ULONGLONG	TotalFileSize;

	CString		strTemp;
	TCHAR		cTemp[MAX_TEMP];

	USES_CONVERSION;
	pcFileName = W2A(ptcFileName);

	pReadFile = fopen(pcFileName, "rb");

	if (pReadFile == NULL)
	{
		strTemp.Format(_T("Failed to open file %s"), pcFileName);
		_tcscpy(cTemp, strTemp);
		LogOut(LEVEL_MSG, cTemp);
	}

	fseek(pReadFile, 0, SEEK_END);
	TotalFileSize = ftell(pReadFile);
	if (fileSize > TotalFileSize)
	{
		strTemp = _T("The read data size is over the total size");
		_tcscpy(cTemp, strTemp);
		LogOut(LEVEL_MSG, cTemp);
	}

	rewind(pReadFile);

	fread(pData, sizeof(unsigned char), fileSize, pReadFile);

	fclose(pReadFile);
	pReadFile = NULL;

	return iRet;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值