读取超大文件的大小(20G),并切分的处理

#include <time.h>
#include <stdio.h>
#include <stdarg.h>
#include <Windows.h>
#include <sys/stat.h>

#define MAX_FILE_LENGTH (500*1024*1024)

__int64 getFileSize(FILE* file) {
    struct _stat64 st;

    if (_fstat64(_fileno(file), &st) == 0)
        return st.st_size;

    return -1;
}

int main()
{
    FILE* file = fopen("D:\\frame.log", "r");
    if (file == NULL) {
        printf("open file failed");
        return 0;
    }

    __int64 size = getFileSize(file);
    printf("file size:%lld Bytes\n", size);

    char strFileName[1024];

    int length = 0;
    char buf[1024];
    int ret = 0;

    int order = 0;
    FILE* pOutFile = NULL;
    int tempLength = 0;
    while (!feof(file)) {
        ret = fread(buf, 1, 1024, file);
        if (ret > 0) {
            if (!pOutFile) {
                order++;
                sprintf(strFileName, "D:\\frame\\%d.log", order);
                pOutFile = fopen(strFileName, "w+");
                tempLength = 0;
                printf("write the file name is %s\n", strFileName);
            }

            if (pOutFile) {
                fwrite(buf, 1, ret, pOutFile);
            } else {
                printf("meet an error when write file\n");
            }

            tempLength += ret;
            if (tempLength >= MAX_FILE_LENGTH) {
                fclose(pOutFile);
                pOutFile = NULL;
            }
        }
    }

    fclose(file);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值