#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;
}
|