SLIDE1

Thursday, April 30, 2015

code c++ phần mềm split and join file part,001,002

code c++ phần mềm split and join file part,001,002



#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>
void layten(char *duongdan,char *ten)
{
 int d=0;
 for(int i=strlen(duongdan)-1;i>=0;i--)
  if(duongdan[i]!='/' && duongdan[i]!='\\') ten[d++]=duongdan[i];
  else break;
 ten[d]='\0';
 for(int i=0;i<strlen(ten)/2;i++)
 {
  char c=ten[i];
  ten[i]=ten[strlen(ten)-1-i];
  ten[strlen(ten)-1-i]=c;
 }
}
void bang(char *a,char *b)
{
 for(int i=0;i<strlen(b);i++) a[i]=b[i];
 a[strlen(b)-1]='\0';
}
int insertpass(char *duongdan,char *mk)
{
 FILE *t=fopen(duongdan,"rb+");
 FILE *f=fopen("logTKT.bin","wb+");
 if(t==NULL || f==NULL) return 0;
 fwrite(mk,8,1,f);
 char s[2];
 while(fread(s,1,1,t)) fwrite(s,1,1,f);
 rewind(t);rewind(f);
 while(fread(s,1,1,f)) fwrite(s,1,1,t);
 fcloseall();
 remove("logTKT.bin");
 return 1;
}
int pass_ok(char *s)
{
 int dem=0;
 for(int i=0;i<strlen(s);i++)
  if(s[i]>='0' && s[i]<='9') dem++;
 if(dem!=strlen(s) || dem!=6) return 0;
 s[6]=s[7]='#';
 s[8]='\0';
 return 1;
}
int split(char *duongdan,char *thumuc,int n)
{
 char ten[100],duongdan2[100],a[10],*s=new char[2];
 unsigned long dungluong,dem;
 int i;
 layten(duongdan,ten);
 strcat(thumuc,"/");
 strcat(thumuc,ten);
 strcat(thumuc,".partt");
 FILE *t=fopen(duongdan,"rb");
 if(t==NULL)
 {
  printf("\nduong dan file khong hop le!");
  return 0;
 }
 fseek(t,0,2);
 dungluong=ftell(t)/n;
 rewind(t);
 for(i=1;i<n;i++)
 {
  bang(duongdan2,thumuc);
  a[0]='\0';
  itoa(i,a,10);
  strcat(duongdan2,a);
  FILE *f=fopen(duongdan2,"wb");
  if(f==NULL)
  {
   printf("\nduong dan thu muc chua file ket qua khong hop le!");
   return 0;
  }
  dem=0;
  do{
   fread(s,1,1,t);
   fwrite(s,1,1,f);
   dem+=1;
  }while(dem!=dungluong);
  fclose(f);
 }
 bang(duongdan2,thumuc);
 a[0]='\0';
 itoa(n,a,10);
 strcat(duongdan2,a);
 FILE *f=fopen(duongdan2,"wb");
 if(f==NULL)
 {
  printf("\nduong dan thu muc chua file ket qua khong hop le!");
  return 0;
 }
 while(fread(s,1,1,t)) fwrite(s,1,1,f);
 fclose(f);
 delete []s;
 fclose(t);
 return 1;
}
int join(char *duongdan,char *thumuc)
{
 int i;
 char ten[100],duongdanketqua[100],a[10],s[2],dd[100];
 layten(duongdan,ten);
 for(i=strlen(ten)-1;i>=0;i--)
  if(ten[i]!='.') ten[i]='\0';
  else break;
 ten[i]='\0';
 duongdanketqua[0]='\0';
 strcat(duongdanketqua,thumuc);
 if(strlen(duongdanketqua)>0)strcat(duongdanketqua,"/");
 strcat(duongdanketqua,ten);
 FILE *t=fopen(duongdanketqua,"wb");
 if(t==NULL)
 {
  printf("\nduong dan thu muc chua file ket qua khong hop le!\n");
  return 0;
 }
 for(i=strlen(duongdan)-1;i>=0;i--)
  if(duongdan[i]>='0' && duongdan[i]<='9') duongdan[i]='\0';
  else break;
 i=1;
 while(1)
 {
  a[0]='\0';
  itoa(i++,a,10);
  bang(dd,duongdan);
  strcat(dd,"t");
  strcat(dd,a);
  FILE *f=fopen(dd,"rb");
  if(i==1 && f==NULL)
  {
   printf("\nduong dan file khong hop le!");
   return 0;
  }
  if(f==NULL) break;
  while(fread(s,1,1,f)) fwrite(s,1,1,t);
  fclose(f);
 }
 fclose(t);
 return 1;
}
int split2(char *duongdan,char *thumuc,char *dl)
{
 unsigned long dungluong=0,dem;
 int i;
 char a[100],ten[100],duongdan2[100],s[2];
 layten(duongdan,ten);
 if(strlen(thumuc)>0)strcat(thumuc,"/");
 strcat(thumuc,ten);
 strcat(thumuc,".partt");
 for(i=0;i<strlen(dl);i++)
  if(dl[i]>='0' && dl[i]<='9') a[i]=dl[i];
  else break;
 a[i]='\0';
 dungluong+=atol(a);
 if(dl[i]=='K') dungluong*=1024;
 if(dl[i]=='M') dungluong*=1024*1024;
 FILE *t=fopen(duongdan,"rb");
 if(t==NULL)
 {
  printf("\nduong dan file khong hop le!");
  return 0;
 }
 i=1;
 while(!feof(t))
 {
  bang(duongdan2,thumuc);
  a[0]='\0';
  itoa(i++,a,10);
  strcat(duongdan2,a);
  FILE *f=fopen(duongdan2,"wb");
  if(f==NULL)
  {
   printf("\nduong dan thu muc chua file ket qua khong hop le!");
   return 0;
  }
  dem=0;
  for(dem=1;dem<=dungluong;dem++)
   if(fread(s,1,1,t)) fwrite(s,1,1,f);
   else break;
  fclose(f);
 }
 return 1;
}
int co_pass(char *duongdan,char *pass)
{
 FILE *t=fopen(duongdan,"rb+");
 if(t==NULL) return -1;
 char s[10];
 fread(s,8,1,t);
 if(s[6]=='#' && s[7]=='#')
 {
  int i;
  for(i=0;i<6;i++) pass[i]=s[i];
  pass[i]='\0';  
  fclose(t);
  return 1;
 }
 fclose(t);
 return 0;
}
void xuly_pass(char *duongdan)
{
 FILE *t=fopen(duongdan,"rb");
 FILE *f=fopen("logTKT.bin","wb+");
 char s[10];
 fread(s,8,1,t);
 while(fread(s,1,1,t)) fwrite(s,1,1,f);
 rewind(f);fclose(t);
 t=fopen(duongdan,"wb");
 while(fread(s,1,1,f)) fwrite(s,1,1,t);
 fcloseall();
 remove("logTKT.bin");
}
void main()
{
 char duongdan[100],thumuc[100],dungluong[100],pass[100];
 int n,k=1,kt,kiemtrapass;
 while(k!=0)
 {
  system("cls");
  printf("\n\n________________MENU___________________\n");
  printf("| 1. chia file thanh n part.(split1)   |\n");
  printf("| 2. chia file theo dung luong.(split2)|\n");
  printf("| 3. gop file (join)                   |\n");
  printf("| 0. Thoat.                            |\n");
  printf("________________________________________\n");
  scanf("%d",&k);



  if(k==1)
  {
   kiemtrapass=0;
   printf("\nnhap vao DUONG DAN FILE chua file can Split: ");fflush(stdin);gets(duongdan);
   printf("\nnhap vao THU MUC chua file ket qua Split: ");fflush(stdin);gets(thumuc);
   printf("\nnhap vao SO PART can Split: N=");scanf("%d",&n);
   printf("\nDo you Set PassWord?? 1-yes; 0-no;  : ");scanf("%d",&kt);
   if(kt==1)
   {
    kiemtrapass=1;
    int kc,kl;
    do{
     kc=0;
     printf("\nnhap 6 chu so password: ");fflush(stdin);gets(pass);
     kl=pass_ok(pass);
     if(kl==0)
     {
      printf("\nnhap pass sai dinh dang! nhap lai!");
      kc=1;
     }
    }while(kc==1);
    printf("\ndang cai dat PassWord vao file! doi nhe! ...");
    kc=insertpass(duongdan,pass);
    if(kc==0)
    {
     printf("\nduong dan file khong hop le!");
     goto ketthuc1;
    }
   }
   printf("\nDANG XU LY! VUI LONG DOI! ...");
   kt=split(duongdan,thumuc,n);
   if(kiemtrapass==1)xuly_pass(duongdan);
   if(kt==1) printf("\nOK - Da Xong!\n");
ketthuc1:;
   system("pause");
  }




  else if(k==2)
  {
   kiemtrapass=0;
   printf("\nnhap vao DUONG DAN FILE chua file can Split: ");fflush(stdin);gets(duongdan);
   printf("\nnhap vao THU MUC chua file ket qua Split: ");fflush(stdin);gets(thumuc);
   
   do{
    kt=0;
    printf("\nnhap vao DUNG LUONG 1 part can Split: dung luong =");fflush(stdin);gets(dungluong);
    int dem=0;
    for(int i=0;i<strlen(dungluong);i++)
     if((dungluong[i]>='0' && dungluong[i]<='9') || dungluong[i]=='B' || dungluong[i]=='K' || dungluong[i]=='M')
      dem++;
    if(dem!=strlen(dungluong))
    {
     printf("\nkhong hop le!");
     kt=1;
    }
   }while(kt);

   printf("\nDo you Set PassWord?? 1-yes; 0-no;  : ");scanf("%d",&kt);
   if(kt==1)
   {
    kiemtrapass=1;
    int kc,kl;
    do{
     kc=0;
     printf("\nnhap 6 chu so password: ");fflush(stdin);gets(pass);
     kl=pass_ok(pass);
     if(kl==0)
     {
      printf("\nnhap pass sai dinh dang! nhap lai!");
      kc=1;
     }
    }while(kc==1);
    printf("\ndang cai dat PassWord vao file! doi nhe! ...");
    kc=insertpass(duongdan,pass);
    if(kc==0)
    {
     printf("\nduong dan file khong hop le!");
     goto ketthuc2;
    }
   }
   printf("\nDANG XU LY! VUI LONG DOI! ...");
   kt=split2(duongdan,thumuc,dungluong);
   if(kiemtrapass==1)xuly_pass(duongdan);
   if(kt==1) printf("\nOK - Da Xong!\n");
ketthuc2:;
   system("pause");
  }



  else if(k==3)
  {
   printf("\nnhap vao DUONG DAN FILE part1: ");fflush(stdin);gets(duongdan);
   printf("\nnhap vao THU MUC chua file ket qua: ");fflush(stdin);gets(thumuc);
   char lay_pass[100];
   printf("\ndang xu ly! ...");
   int mk=co_pass(duongdan,lay_pass);
   if(mk==-1)
   {
    printf("\nduong dan file khong hop le!");
    goto ketthuc3;
   }
   else if(mk==1)
   {
    char check_pass[100];
    printf("\nnhap PASS de join file: ");fflush(stdin);gets(check_pass);
    if(strcmp(lay_pass,check_pass)!=0)
    {
     printf("\nnhap pass sai!!!");
     goto ketthuc3;
    }
    else xuly_pass(duongdan);
   }
   printf("\nDANG XU LY! VUI LONG DOI! ...");
   kt=join(duongdan,thumuc);
   if(kt==1) printf("\nOK - Da Xong!\n");
ketthuc3:;
   system("pause");
  }
  else printf("\n\nTAM BIET!\n");
 }
 getch();
}