文件大小和文件长度的区别

1.获取文件大小:

S64 OsalFileSizeGet(char *apFile)
{
    struct stat buf;
    if(stat(apFile,&buf)<0)
        return 0;
    else
        return buf.st_size;
}

2.获取文件长度:

S32 OsalFileLenGet(FILE *pFile)
{
    if (NULL == pFile)
        return 0;
    
    /* Save the current position. */
    int save_pos = ftell( pFile );
    
    /* Jump to the end of the file. */
    fseek( pFile, 0L, SEEK_END );
    
    /* Get the end position. */
    int fileLen = ftell( pFile );
    
    /* Jump back to the original position. */
    fseek( pFile, save_pos, SEEK_SET );


    return fileLen;
}

3.判断文件是否存在:

int IsFileExist(char *apFile)
{
    if (access(apFile, R_OK|W_OK))
        return 0;
    else
        return 1;
}

4.合并文件2到文件1中:

S32 OsalMergeFile(char *pFilePath1, char *pFilePath2)
{
    FILE *fp1 = fopen(pFilePath1 , "a+");
    if (NULL == fp1)
    {
        LOG_ERR(MOD_SSI, "fail to open file1: %s err:%s\n", pFilePath1, strerror(errno));
        return RERROR;
    }
    
    FILE *fp2 = fopen(pFilePath2 , "r");
    if (NULL == fp2)
    {
        LOG_ERR(MOD_SSI, "fail to open file2: %s err:%s\n", pFilePath2, strerror(errno));
        fclose(fp1);
        return RERROR;
    }


    int rc;
    unsigned char buf[MAX_FILE_SIZE];
    while( (rc = fread(buf, 1, MAX_FILE_SIZE, fp2)) != 0)
    {
        fwrite( buf, 1, rc, fp1);
    } 



    fclose(fp1);
    fclose(fp2);
    return ROK;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值