int wow_file_remove(const char *pcFile)
{
CHECK_RET_VAL_P(pcFile,-PARAM_INPUT_DATA_IS_NULL,"param input data invalid!\n");
return remove(pcFile);
}
检查文件是否存在
int wow_file_check_exist(const char *pcFile)
{
CHECK_RET_VAL_P(pcFile,-PARAM_INPUT_DATA_IS_NULL,"param input data invalid!\n");
return (access(pcFile, F_OK|W_OK|R_OK) == 0) ? 1 : 0;
}
文件重命名
int wow_file_rename(const char *pcSrcFile, const char *pcDestFile)
{
CHECK_RET_VAL_P(pcSrcFile && pcDestFile,-PARAM_INPUT_DATA_IS_NULL,"param input data invalid!\n");
return rename(pcSrcFile, pcDestFile);
}
获取文件大小
int wow_file_size(const char *pcFile,int* pnSize)
{
CHECK_RET_VAL_P(pcFile,-PARAM_INPUT_DATA_IS_NULL,"param input data invalid!\n");
struct stat st;
int ret = stat(pcFile, &st);
CHECK_RET_VAL_P_A(ret == 0, -SYSTEM_STAT_FAILED,"file stat failed!\n");
*pnSize = st.st_size;
return WOW_SUCESS;
}
检查文件行数
int wow_file_line_num(const char *pcFile,int* pnLine)
{
CHECK_RET_VAL_P(pcFile,-PARAM_INPUT_DATA_IS_NULL,"param input data invalid!\n");
int ch;
int lineCnt = 0;
FILE *fp = fopen(pcFile, "r");
CHECK_RET_VAL_P(fp,-SYSTEM_FOPEN_FAILED,"file open failed!\n");
while((ch = fgetc(fp)) != EOF)
{
if(ch == '\n')
{
lineCnt++;
}
}
fclose(fp);
*pnLine = lineCnt;
return WOW_SUCESS;
}
两个文件比较
/*内容相同返回0 不相同返回1 错误参数返回< 0*/
int wow_file_compare(const char* pcFile1,const char* pcFil2)
{
int ret = 0;
CHECK_RET_VAL_P(pcFile1 && pcFil2,-PARAM_INPUT_DATA_IS_NULL,"param input data invalid!\n");
FILE* const fp1 = fopen(pcFile1, "rb");
FILE* const fp2 = fopen(pcFil2, "rb");
CHECK_RET_VAL_P(fp1 && fp2,-SYSTEM_FOPEN_FAILED,"file open failed!\n");
while (ret == 0) {
char b0[1024];
char b1[1024];
size_t const r0 = fread(b0, 1, sizeof(b0), fp1);
size_t const r1 = fread(b1, 1, sizeof(b1), fp2);
ret = (r0 != r1);
if (!r0 || !r1) break;
if (!ret) ret = memcmp(b0, b1, r0);
fclose(fp1);
fclose(fp2);
}
return ret;
}