Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1096729
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

分类: C/C++

2010-04-09 10:40:39

#include <iostream>

#include <netinet/in.h>
#include <sys/socket.h>
#include <wchar.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <math.h>
#include <dirent.h>

#define SPLITSIZE ((1 << 10) * 100)        // 100K


using namespace std;

static void split(const char *filename)
{
    char buff[(1 << 10) * 100], piece[4096];
    int len;
    int sfd, dfd;    
    int count = 0, suffix = 0;        // count is each piece size, suffix is piece suffix

    
    if ((sfd = open(filename, O_RDONLY)) == -1) {
        perror("open");
        return;
    }
    
    for (;;) {
        count = SPLITSIZE;
        suffix++;

        memset(piece, '\0', sizeof(piece));
        snprintf(piece, sizeof(piece) - 1, "%s%d", "/home/fanyf/work/cppwork/split/piecedir/", suffix);
        if ((dfd = open(piece, O_CREAT | O_WRONLY | O_TRUNC, 0666)) == -1) {
            perror("open");
            return;
        }

        while (count > 0) {        // read 100K data

            memset(buff, '\0', sizeof(buff));
            if ((len = read(sfd, buff, count)) == -1) {
                perror("read");
                goto out;
            } else if (len == 0) {    // read over

                close(dfd);
                goto out;
            }

            if (write(dfd, buff, len) != len) {
                perror("write");
                goto out;
            }

            count -= len;
        }
        close(dfd);
    }
out:
    close(sfd);
}

static void purge(const char *dir)
{
    char filename[1024], absolute_path[4096];
    DIR *pdir;
    struct dirent *dentry;

    if ((pdir = opendir(dir)) == NULL) {
        perror("opendir");
        return;
    }

    while ((dentry = readdir(pdir)) != NULL) {
        memset(absolute_path, '\0', sizeof(absolute_path));
        snprintf(absolute_path, sizeof(absolute_path) - 1, "%s/%s", dir, dentry->d_name);
        unlink(absolute_path);
    }
    closedir(pdir);
}

int main()
{
    time_t stime, etime;
    
    time(&stime);
    split("/home/fanyf/work/cppwork/split/doc.txt");
    purge("/home/fanyf/work/cppwork/split/piecedir");
    time(&etime);
    printf("elapse: %d\n", etime - stime);

    return 0;
}


阅读(1132) | 评论(0) | 转发(0) |
0

上一篇:c++双向链表

下一篇:clucene example 2

给主人留下些什么吧!~~