博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

Linuxwhite

Keep Walking...... Because I will be there.

linuxwhite.cublog.cn


文件分割合并器-C实现

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <malloc.h>

char nameinputt[80];
char namebefdot[76];
char nameaftdot[20];
char num[60][5]={
      {"1"},{"2"},{"3"},{"4"},{"5"},{"6"},
     {"7"},{"8"},{"9"},{"10"},{"11"},{"12"},
         {"13"},{"14"},{"15"},{"16"},{"17"},{"18"},
         {"19"},{"20"},{"21"},{"22"},{"23"},{"24"},
         {"25"},{"26"},{"27"},{"28"},{"29"},{"30"},
         {"31"},{"32"},{"33"},{"34"},{"35"},{"36"},
         {"37"},{"38"},{"39"},{"40"},{"41"},{"42"},
         {"43"},{"44"},{"45"},{"46"},{"47"},{"48"},
         {"49"},{"50"},{"51"},{"52"},{"53"},{"54"},
         {"55"},{"56"},{"57"},{"58"},{"59"},{"60"}};
char namenew[60][80]={{""}};
char namefile[61][80]={{""}};
char namedest[61][80];

void choose();
void split();
void unite();
void destname();
int changename();
void savename();
int readname();
int atoi();

void destname(int f)
{int i,j,k;
    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],num[f]);
    strcat(namenew[f],nameaftdot);
}

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

void savename(int count)
{FILE *fp ;
  int i,j;

 if((fp = fopen("file", "w"))==NULL)
 {printf(" file文件不在此目录\n");exit(0);}
 for(j=0;j<80;j++)
    namefile[count][j]=nameinputt[j];    
 for(i=0 ; i <=count+1 ; i++)
     {
      fprintf(fp, "%s\n", namefile[i]) ;
      }
  fclose(fp) ;
}
 
int readname()
{ FILE *fp ;
  int i = 0 ,j;
  int count;
  char *p ;
  
  
  if((fp = fopen("file", "r")) == NULL) {printf(" file文件不在此目录\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++;}
return count-1; }

void choose()
{int flag=0;
 char choice[2];
 int swi;
    printf("-------------------------------------------------------------------------------\n");
    printf(" 你要做什么?\n");
    printf("-------------------------------------------------------------------------------\n");
    printf(" 1.合并\n");
    printf(" 2.分割\n");
    printf(" 3.离开\n");
    printf("-------------------------------------------------------------------------------\n");
    do {swi=atoi(gets(choice));
     if(swi==1) unite();
        else if(swi==2) split();
        else if(swi==3) exit(0);
        else {
              printf("-------------------------------------------------------------------------------\n");
         printf(" 输入错误,请输入1或2或3\n");
              printf("-------------------------------------------------------------------------------\n");
             flag=1;
        }
    }while(flag==1);
}

void split()
{ FILE *in,*out;
    void *buffer;
    int blocks,f=0,k;

    printf("-------------------------------------------------------------------------------\n");
    printf(" 输入你想要分割文件的文件名\n");
    printf("-------------------------------------------------------------------------------\n");
    scanf("%s",nameinputt);
   if((in=fopen(nameinputt,"rb"))==NULL)
    {
    printf("-------------------------------------------------------------------------------\n");
    printf(" 不能打开被分割文件,请检查输入的文件名\n");
    printf("-------------------------------------------------------------------------------\n");
    choose(0);}
    buffer=calloc(51200,1024);
    printf(" 分割进行中\n");
    printf("-------------------------------------------------------------------------------\n");
    printf(" 分割后的文件是\n");
    while((blocks=fread(buffer,1024,51200,in))>0)
    {destname(f);
    for(k=0;k<80;k++)
    namefile[f][k]=namenew[f][k];
    printf(namenew[f]);
    printf("\n");
    if((out=fopen(namenew[f],"wb"))==NULL)
     printf("can't open out file\n");
     if(blocks==51200)
         fwrite(buffer,1024,51200,out);
     else fwrite(buffer,1024,blocks+1,out);
     f=changename(f);
        fclose (out);}
    fclose(in);
    free (buffer);
    savename(f);
    printf(" 分割完毕\n");
    printf("-------------------------------------------------------------------------------\n");
    choose();
}

void unite()
{ int i=0,blocks;
      int count=0;
 FILE *creat,*file;
    void *buffer;
    
    printf(" 合并进行中\n");
    printf(" 请稍后\n");
    printf("-------------------------------------------------------------------------------\n");
    count=readname();
       if((creat=fopen(namedest[count],"wb"))==NULL)
        printf("can't create file\n");
    fclose(creat);
    if((creat=fopen(namedest[count],"ab"))==NULL)
        printf("can't open created file\n");
    buffer=calloc(51200,1024);
    do{if((file=fopen(namedest[i],"rb"))==NULL)
        printf("can't open united file\n");
     if((blocks=fread(buffer,1024,51200,file))==51200)
         fwrite(buffer,1024,51200,creat);
     else fwrite(buffer,1024,blocks,creat);
     fclose(file);
     i++;
     }while(i<count);
    fclose(creat);
    free(buffer);
    printf(" 已成功,合并后的文件是:\n");
    printf(namedest[count]);
    printf("\n");
    choose();
}

int main()
{printf(" *>大影视文件分割合并器V1.1版<*\n");
 printf(" *>此程序可以把大于50M的文件分割成几个50M以下的小文件<*\n");
 printf(" *>并且可以合并由它分割生成的文件<*\n");
 printf(" *>秦川 2006<*\n");
 printf(" *>Email:linuxqc#gmail.com<*\n");
 printf("-------------------------------------------------------------------------------\n");
 printf(" *>注意请把file文件、分割后的文件一起拷贝过去,和程序放在同一目录,再合并<*.\n");
 choose();}

发表于: 2008-03-16 ,修改于: 2008-03-23 11:07,已浏览936次,有评论1条 推荐 投诉


网友评论
内容:
The exit(); is a buid-in function which type is void in Linux OS. I delete the line"int exit();" and add  include<stdlib.h> to avoid warnings by gcc today.

linuxwhite 评论于:2008-03-23 00:14:24 (121.62.164.★)

发表评论