标签:++i param cfile rem const get while win lin
源代码:#include <stdio.h>
#include <stdlib.h>
#define SUCCESS 0
#define FAILURE 1
#define IN
#define OUT
#define INOUT
typedef unsigned long BOOL_T;
typedef unsigned char UCHAR;
typedef unsigned long ULONG;
#define BOOL_TRUE (BOOL_T)1
#define BOOL_FALSE (BOOL_T)0
static inline FILE* fopen_s(IN const char *fname, IN const char *mode)
{
FILE *pcFile = NULL;
if (NULL == fname || NULL == mode)
{
fprintf(stderr, "line:%d,wrong param [fname or mode = NULL]\n", __LINE__);
return NULL;
}
pcFile = fopen(fname, mode);
if (NULL == pcFile)
{
fprintf(stderr, "line:%d,fopen() failed", __LINE__);
return NULL;
}
return pcFile;
}
ULONG hex2Dec(IN UCHAR aucHex[], OUT UCHAR *pucDec)
{
ULONG i = 0;
ULONG weight = 0;
BOOL_T bflag = BOOL_TRUE;
if (NULL == pucDec)
{
fprintf(stderr, "line:%d,wrong param [pucDec = NULL]\n", __LINE__);
return FAILURE;
}
*pucDec = 0;
for (i = 0; i < 2; ++i)
{
weight = bflag ? 16 : 1;
if (‘a‘ <= aucHex[i] && ‘f‘ >= aucHex[i])
{
*pucDec += (UCHAR)((aucHex[i] - ‘a‘ + 10) * weight);
}
else if (‘0‘ <= aucHex[i] && ‘9‘ >= aucHex[i])
{
*pucDec += (UCHAR)((aucHex[i] - ‘0‘) * weight);
}
else
{
return FAILURE;
}
bflag = BOOL_FALSE;
}
return SUCCESS;
}
ULONG removeSpInFile(IN const char *pcFilePath)
{
FILE *pcOriginalFile = NULL;
FILE *pcTmpFile = NULL;
int ich = 0;
int iRet = 0;
if (NULL == pcFilePath)
{
fprintf(stderr, "line:%d,wrong param [pcFilePath = NULL]\n", __LINE__);
return FAILURE;
}
pcOriginalFile = fopen_s(pcFilePath, "r");
//创建临时文件
pcTmpFile = tmpfile();
if (NULL == pcTmpFile)
{
fprintf(stderr, "line:%d,tmpfile() failed\n", __LINE__);
fclose(pcOriginalFile);
return FAILURE;
}
while (EOF != (ich = fgetc(pcOriginalFile)))
{
if (‘ ‘ == ich || ‘\n‘ == ich)
{
continue;
}
fputc(ich, pcTmpFile);
}
fclose(pcOriginalFile);
iRet = remove(pcFilePath);
if (SUCCESS != iRet)
{
fprintf(stderr, "line:%d,remove() failed\n", __LINE__);
fclose(pcTmpFile);
return FAILURE;
}
pcOriginalFile = fopen_s(pcFilePath, "w");
rewind(pcTmpFile);
while (EOF != (ich = fgetc(pcTmpFile)))
{
fputc(ich, pcOriginalFile);
}
fclose(pcOriginalFile);
fclose(pcTmpFile);
return SUCCESS;
}
ULONG txt2bin(IN const char *pcFilePath)
{
FILE *pcOriginalFile = NULL;
FILE *pcTmpFile = NULL;
ULONG ulRet = SUCCESS;
UCHAR aucHex[2] = {0};
UCHAR ucDec = 0;
int ich = 0;
ulRet = removeSpInFile(pcFilePath);
if (SUCCESS != ulRet)
{
fprintf(stderr, "line:%d,removeSpInFile() failed\n", __LINE__);
return FAILURE;
}
pcOriginalFile = fopen_s(pcFilePath, "rb");
//创建临时文件
pcTmpFile = tmpfile();
if (NULL == pcTmpFile)
{
fprintf(stderr, "line:%d,tmpfile() failed\n", __LINE__);
fclose(pcOriginalFile);
return FAILURE;
}
while (EOF != fscanf(pcOriginalFile, "%c%c", &aucHex[0], &aucHex[1]))
{
ulRet = hex2Dec(aucHex, &ucDec);
if (SUCCESS != ulRet)
{
fprintf(stderr, "line:%d,hexToDec() failed\n", __LINE__);
return FAILURE;
}
fputc(ucDec, pcTmpFile);
}
fclose(pcOriginalFile);
ulRet = (ULONG)remove(pcFilePath);
if (SUCCESS != ulRet)
{
fprintf(stderr, "line:%d,remove() failed\n", __LINE__);
fclose(pcTmpFile);
return FAILURE;
}
pcOriginalFile = fopen_s(pcFilePath, "wb");
rewind(pcTmpFile);
while (EOF != (ich = fgetc(pcTmpFile)))
{
fputc(ich, pcOriginalFile);
}
fclose(pcOriginalFile);
fclose(pcTmpFile);
return SUCCESS;
}
int main(int argc, char *argv[])
{
ULONG ulRet = 0;
if (2 != argc)
{
fprintf(stderr, "Usage %s <fname>\n", argv[1]);
return FAILURE;
}
ulRet = txt2bin(argv[1]);
if (SUCCESS != ulRet)
{
fprintf(stderr, "line:%d,txt2bin() failed\n", __LINE__);
return FAILURE;
}
return SUCCESS;
}
标签:++i param cfile rem const get while win lin
原文地址:http://blog.51cto.com/bocaihaoci/2068503