编码规范初体验
Posted 是光哥呀
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编码规范初体验相关的知识,希望对你有一定的参考价值。
基于上文进行编程规范:上文.
#include <stdio.h>
#include <stdlib.h>
/*封装一个接口,把数据写入到文件里*/
/*1.注意函数的重用性
2.注意对于参数的判断
3.注意格式规范
*/
int saveDataToFile(unsigned char *dataAddr,long dataSize,
const char *fileName)
{
int nret=0;
FILE *fp=NULL;
if (!dataAddr || !fileName)
{
printf("wrong dataAddr or fileName\\n");
return -1;
}
if (dataSize <= 0)
{
printf("wrong dataSize\\n");
return -2;
}
fp=fopen(fileName,"wb+");
if (!fp)
{
printf("open file wrong\\n");
return -3;
}
nret = fwrite(dataAddr,1,dataSize,fp);
if (nret < 0)
{
printf("write failed\\n");
return -4;
}
fclose(fp);
return 0;
}
/*Y分量数据的值减半 我们也封装成一个接口*/
int halfDataY(const char *dstFileName,const char *srcFileName,
long dataSize,long ydataSize)
{
int nRet = 0;
int nIdx= 0;
//如果是unsigned int则命名为dwIdx
unsigned char *dataAddr = NULL;
FILE *fp;
if (!srcFileName || !dstFileName)
{
printf("input fileName wrong\\n");
return -1;
}
if (dataSize <= 0)
{
printf("input dataSize wrong\\n");
return -2;
}
dataAddr = (unsigned char *)malloc(dataSize);
fp = fopen(srcFileName,"rb+");
if (!fp)
{
printf("open file failed\\n");
return -3;
}
nRet = fread(dataAddr,1,dataSize,fp);
if (nRet < 0)
{
printf("read file failed\\n");
return -4;
}
for (nIdx = 0;nIdx < ydataSize; nIdx++)
{
dataAddr[nIdx] = dataAddr[nIdx]/2;
}
nRet = saveDataToFile(dataAddr,dataSize,dstFileName);
if (nRet)
{
printf("saveData failed,nRet=%d\\n",nRet);
}
free(dataAddr);
fclose(fp);
return 0;
}
int main()
{
int nRet = 0;
halfDataY("new_256x256_yuv420p.yuv","lena_256x256_yuv420p.yuv",
256*256*3/2,256*256);
if (nRet)
{
printf("halfDataY failed,nRet=%d\\n",nRet);
return -1;
}
return 0;
}
以上是关于编码规范初体验的主要内容,如果未能解决你的问题,请参考以下文章