Chinaunix首页 | 论坛 | 博客
  • 博客访问: 370727
  • 博文数量: 66
  • 博客积分: 3201
  • 博客等级: 中校
  • 技术积分: 695
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-04 11:17
文章分类

全部博文(66)

文章存档

2016年(1)

2014年(1)

2012年(1)

2011年(2)

2010年(18)

2009年(42)

2008年(1)

分类: C/C++

2010-08-13 17:51:11


主要是为了合并一个src目录里符合"×.300"的文件,合并出来的文件大小超过PERSIZE就把下面的合并到另外的文件里。

程序保证数据不丢失,所以可能有超大的文件还是会写入一个文件的。





/**
 * @brief
 *
 * @author jervis
 * @time 13/08/10 13:55:52
 * @version 0.1
 *
 * Copyright (C) jervis
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>

#define PERSIZE 1024 * 1024 * 10
int CombineFiles(char *src, char *deal_busi, char *deal_date)
{
    DIR *dirp;
    struct dirent *direntp;
    FILE *fpsrc = NULL, *fpdest = NULL;
    char dest[100];
    int num = 0;
    struct stat sbuf;
    char buf[256];
    char *fbuf = NULL;
    char *lstp = NULL;
    unsigned long total = 0;

    if ((dirp = opendir(src)) == NULL) {
        perror("opendir");
        return 1;
    }

    memset(dest, 0, sizeof(dest));
    sprintf(dest, "%s/%s", src, "tmp");
    if (access(dest, F_OK) < 0) {
        mkdir(dest, 0755);
    }

    while ((direntp = readdir(dirp))) {
        if ((strncmp(direntp->d_name, ".", 1) == 0) || (strncmp(direntp->d_name, "..", 2) == 0))
            continue;
        if ((lstp = strrchr(direntp->d_name, '.')) != NULL) {
            if(strncmp(lstp + 1, deal_busi, strlen(deal_busi)) != 0) {
                continue;
            }
        } else {
            continue;
        }
        memset(&sbuf, 0, sizeof(sbuf));

        memset(buf, 0, sizeof(buf));
        sprintf(buf, "%s/%s", src, direntp->d_name);
        if (stat(buf, &sbuf) == -1) {
            perror("stat");
            return 1;
        }
        total += sbuf.st_size;
        if (sbuf.st_size == 0) {
            continue;
        } else if (sbuf.st_size > PERSIZE || total > PERSIZE) {
            /*当单个文件写入大小累计大于PERSIZE时,关闭,开新的再来*/
            if (fpdest != NULL) {
                fclose(fpdest);
                fpdest = NULL;
                total = 0;
                num++;
            }
        }
        if (fpdest == NULL) {
            memset(buf, 0, sizeof(buf));
            sprintf(buf, "%s/%s%04d.%s", dest, deal_date + 2, num, deal_busi);
            if ((fpdest = fopen(buf, "a")) == NULL) {
                perror("fopen");
                return 1;
            }
        }
        fbuf = calloc(sbuf.st_size, sizeof(char));
        if (fbuf == NULL) {
            perror("calloc");
            return 1;
        }
        memset(buf, 0, sizeof(buf));
        sprintf(buf, "%s/%s", src, direntp->d_name);
        if ((fpsrc = fopen(buf, "r")) == NULL) {
            perror("fopen");
            return 1;
        }
        fread(fbuf, sizeof(char), sbuf.st_size, fpsrc);
        fclose(fpsrc);
        fwrite(fbuf, sizeof(char), sbuf.st_size, fpdest);
        free(fbuf);
        fbuf = NULL;
    }
    fclose(fpdest);

    memset(buf, 0, sizeof(buf));
    sprintf(buf, "rm -f %s/*.%s", src, deal_busi);
    system(buf);

    memset(buf, 0, sizeof(buf));
    sprintf(buf, "mv %s/tmp/*.%s %s/ > /dev/null 2>&1 && rmdir %s/tmp > /dev/null 2>&1", src, deal_busi, src, src);
    system(buf);
    return 0;
}

int main(int argc, char* argv[])
{
    CombineFiles("src", ".300", "20100720");
    return 0;
}


阅读(447) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~