Chinaunix首页 | 论坛 | 博客
  • 博客访问: 229506
  • 博文数量: 32
  • 博客积分: 2318
  • 博客等级: 大尉
  • 技术积分: 298
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-14 01:07
个人简介

新博客: http://qinchuan.me

文章分类
文章存档

2012年(2)

2011年(7)

2010年(6)

2009年(8)

2008年(9)

分类: C/C++

2011-02-01 16:50:24

原创文章,转载请注明出处:http://blogold.chinaunix.net/u2/63488/showart_2500710.html by Linuxqc

去掉了前一个版本(http://blog.chinaunix.net/u2/63488/showart_2126194.html)中的一个二维数组。添加了一些新的features。不过,此版本因为涉及到目录处理问题,只在Windows XP下保证测试通过。(VC++6.0)移植到Linux平台可能需要稍作修改。
程序并不完善。结构也很糟糕。计划着v1.2时把结构弄清晰。v1.3版本时,改用C++和面向对象思想实现。v1.3版本之后,可能会同时提供GUI和CUI。
使用方法:
     分割:spliter s < 完整路径文件名 > < 每部分大小(KB)>
     合并:spliter u < 零碎文件和临时数据文件存放的目录 >
     示例:spliter s c:\file 3   表示将存诸在c:\位置的file文件分割成大小为3KB的许多部分。
      spliter u      表示将存诸在c:\ 位置的零碎文件们合并。
     注意:合并时,务必将零碎文件连同生成的临时数据文件 data_file_names、data_file_size 放在同一目录。合并后的文件将出现在零碎文件所在的目录。

/* C source file: spliter.c */

/* Spliter, a program to split and merge files.
 * Written by linuxqc@gmail.com
 * This is version 1.1.3 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char nameinputt[80];        /* The name of the input file */
char namebefdot[76];        /* Parts of the name of the input file before the dot */
char nameaftdot[20];        /* Parts of the name of the input file after the dot */
char namenew[60][80] = { {""} };    /* The final names of the output files */
char namefile[61][80] = { {""} };    /* The data structure for saving the file names in the data_file_names file */
char namedest[61][80];        /* Used by the merge() function to creat file names */

void destname(int f)
{                /* Creat the output files' names */
    int i, j, k;
    char buffer[20];
    for (i = 0; i < 80; i++) {
    if (nameinputt[i] == '.')
     break;
    namebefdot[i] = nameinputt[i];
    }
    for (j = 0, k = i; k < 80 && j < 20; k++, j++) {
        nameaftdot[j] = nameinputt[k];
    }
    strcat(namenew[f], namebefdot);
    strcat(namenew[f], itoa(f, buffer, 10));
    strcat(namenew[f], nameaftdot);
}

int changename(int f)
{
    f += 1;
    return f;
}

long filelen(char *filename)
{                /* Get the size of the file */
    FILE *v_fpLog = NULL;
    long fSet = 0, fEnd = 0;
    long filelen = 0;
    if ((v_fpLog = fopen(filename, "r+")) == NULL) {
        printf("The file '%s' was not opened", filename);
        return 0;
    }
    else {
        fseek(v_fpLog, 0, SEEK_SET);
        fSet = ftell(v_fpLog);
        fseek(v_fpLog, 0, SEEK_END);
        fEnd = ftell(v_fpLog);
        /* file lenght */
        filelen = (fEnd - fSet) / 1024;
    }
    return filelen;
}


void savename(char *nameinputt, int count)
{                /* Save file names in data_file_names */
    FILE *fp;
    int i, j, k, position;
    char full_path_name[255];
    char file_name[255];

    for (i = 0; i < 255; i++) {
        full_path_name[i] = '\0';
    }
    for (i = 0; i < 255; i++) {
        file_name[i] = '\0';
    }
    position = 0;
    i = 0;
    while (nameinputt[i] != '\0') {
        if (nameinputt[i] == '\\') {
            position = i;
        }
        i++;
    }
    for (i = 0; i < position; i++) {
        full_path_name[i] = nameinputt[i];
    }
    strcat(full_path_name, "\\");
    strcat(full_path_name, "data_file_names");

    if ((fp = fopen(full_path_name, "w")) == NULL) {
        printf(" Error: can't create the data file 'data_file_names'.\n");
        exit(0);
    }
    for (j = 0; j < 80; j++)
        namefile[count][j] = nameinputt[j];
    while (nameinputt[i] != '\0') {
        if (nameinputt[i] == '\\') {
            position = i;
        }
        i++;
    }
    for (i = 0; i <= count; i++) {
        j = 0;
        k = position + 1;
        while (namefile[i][k] != '\0') {
            file_name[j] = namefile[i][k];
            k++;
            j++;
        }
        file_name[j] = '\0';
        fprintf(fp, "%s\n", file_name);
    }
    fclose(fp);
}

void savesize(char *nameinputt, long int size)
{                /* Save size of the file in data_file_size */
    FILE *fp;
    int i, position;
    char full_path_size[255];

    for (i = 0; i < 255; i++) {
        full_path_size[i] = '\0';
    }
    position = 0;
    i = 0;
    while (nameinputt[i] != '\0') {
        if (nameinputt[i] == '\\') {
            position = i;
        }
        i++;
    }
    for (i = 0; i < position; i++) {
        full_path_size[i] = nameinputt[i];
    }
    strcat(full_path_size, "\\");
    strcat(full_path_size, "data_file_size");

    if ((fp = fopen(full_path_size, "w")) == NULL) {
        printf(" Error: cant' create the data file 'data_file_size'.\n");
        exit(0);
    }
    fprintf(fp, "%ld", size);
    fclose(fp);
}

int readname(char *directory)
{                /* Get file names from data_file_names */
    FILE *fp;
    int i = 0, j;
    int count;
    char *p;
    char full_file_name[255];

    strcpy(full_file_name, directory);
    strcat(full_file_name, "\\");
    strcat(full_file_name, "data_file_names");
    if ((fp = fopen(full_file_name, "r")) == NULL) {
        printf(" Error: the data file named 'data_file_names' is not in the directory.\n");
        exit(0);
    }
    while (fgets(namedest[i], 80, fp) != NULL) {
        p = strchr(namedest[i], '\n');
        if (p)
            *p = 0;
        i++;
        }
    for (j = 0, count = 0; j < 61; j++) {
        if (namedest[j][0] != '\0')
            count++;
    }
    fclose(fp);
    return count - 1;
}


long int readsize(char *directory)
{                /* Get the size of the file from data_file_size */
    FILE *fp;
    long int size;
    char full_file_name[255];

    strcpy(full_file_name, directory);
    strcat(full_file_name, "\\");
    strcat(full_file_name, "data_file_size");
    if ((fp = fopen(full_file_name, "r")) == NULL) {
        printf(" Error: the data file named 'data_file_size' is not in the directory.\n");
        exit(0);
    }
    fscanf(fp, "%ld", &size);
    fclose(fp);
    return size;
}

int split(char *nameinputt, long int size)
{                /* The function to split */
    FILE *in, *out;
    int i;
    int f = 0, k, times;
    int ch, flag;
    long int len;
    int per;
    if ((in = fopen(nameinputt, "rb")) == NULL) {
        printf("-------------------------------------------------------------------------------\n");
        printf(" Error: can not open input file, please check if you have entered the right file name.\n");
        printf("-------------------------------------------------------------------------------\n");
        exit(0);
    }
    len = filelen(nameinputt);
    printf("-------------------------------------------------------------------------------\n");
    printf("\n Spliting...please wait.\n");
    printf("-------------------------------------------------------------------------------\n");
    printf(" 0%% has been done.\n");
    printf(" The output files are:\n");
    while (!feof(in)) {
        destname(f);
        for (k = 0; k < 80; k++)
            namefile[f][k] = namenew[f][k];
        printf(namenew[f]);
        printf("\t");
        if ((out = fopen(namenew[f], "wb")) == NULL)
            printf("Error: can't open out file\n");
        for (times = 0; times <= size * 1024 && (!feof(in)); times++) {
            ch = fgetc(in);
            flag = (!feof(in));
            if (flag == 1)
            fputc(ch, out);
        }
        per = (f + 1) / (float) (1 + len / size) * 100;
        printf(" %d%% has been done.\n", per);
        f = changename(f);
        fclose(out);
    }
    fclose(in);
    savename(nameinputt, f);
    savesize(nameinputt, size);
    printf(" Done!\n");
    printf("-------------------------------------------------------------------------------\n");
    return 0;
}

int merge(char *directory)
{                /* The function to merge */
    int i = 0;
    int count = 0, times, per;
    long int size = 0;
    int ch, flag;
    FILE *creat, *file;
    char full_file_name[255];

    printf(" Merging...please wait.\n");
    printf("-------------------------------------------------------------------------------\n");
    printf(" 0%% has been done.\n");
    count = readname(directory);
    size = readsize(directory);
    strcpy(full_file_name, directory);
    strcat(full_file_name, "\\");
    strcat(full_file_name, namedest[count]);
    if ((creat = fopen(full_file_name, "wb")) == NULL)
    printf("Error: can't create file\n");
    fclose(creat);

    if ((creat = fopen(full_file_name, "ab")) == NULL)
    printf("Error: can't open created file\n");
    do {
        strcpy(full_file_name, directory);
        strcat(full_file_name, "\\");
        strcat(full_file_name, namedest[i]);
        if ((file = fopen(full_file_name, "rb")) == NULL) {
            printf("Error: can't open splited file\n");
            exit(0);
        }
        for (times = 0; times <= size * 1024 && (!feof(file)); times++) {
            ch = fgetc(file);
            flag = (!feof(file));
            if (flag == 1)
            fputc(ch, creat);
        }
        per = (i + 1) / (float) count *100;
        printf(" %d%% has been done.\n", per);
        fclose(file);
        i++;
    } while (i < count);
    fclose(creat);
    printf(" Done! The file is:\n");
    printf(namedest[count]);
    printf("\n");
    return 0;
}


int main(int argc, char *argv[])
{                /* Human interface */
    int i;
    long int size = 0;

    if ((argc == 1) || (argc == 2) || (argv[1] == "--help")
    || (argv[1] == "/?") || ((argc == 3) && (argv[1][0] != 'u'))
    || ((argc == 4) && (argv[1][0] != 's')) || (argc 
> 4) ) {
        printf("spliter, a program to split and merge files.\nDesigned by linuxqc@gmail.com.\nThis is version 1.1.3.\n\n");
        printf("Usage:\n");
        printf("To split: spliter s < filename > < size of each part measured by Kilo Bytes >\n");
        printf("To merge: spliter u < directory, in which the splited files are stored. >\n\n");
        printf("Example: spliter s file 3\nSplit file to many parts, each of them is 3 KB's big.\n\n");
        printf("Attention: When you merge the many files that you have splited to, please make sure that they are in the same directory with 'data_file_names' and 'data_file_size'.\n\n");
        exit(0);
    }

    else if ((argc == 3) && (argv[1][0] == 'u')) {
        merge(argv[2]);
        exit(0);
    }
    else {
        for (i = 0; i < 256; i++) {
            if (argv[2][i] != '\0')
                nameinputt[i] = argv[2][i];
            else
                break;
        }
        size = atoi(argv[3]);
        split(nameinputt, size);
        exit(0);
    }
    return 0;
}


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