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

来源:互联网 发布:正装鞋 知乎 编辑:程序博客网 时间:2024/04/29 17:56

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