檔案管理系統 (轉)

worldblog發表於2007-12-15
檔案管理系統 (轉)[@more@]

/***********文 件 管 理 系 統***********/
#include
#include   /*不容易歸類的標準庫*/
#include
#include
#include  
#include   /*非標準輸入輸出操作的程式碼符號屬性*/
#include
#include

int init()  /*初始化操作介面函式*/
{int i;
 clrscr();
 gotoxy(23,4);printf("* * * * * * * * * * * * * *");
 gotoxy(27,5);printf("FILE MANAGE SYSTEM");
 gotoxy(23,6);printf("* * * * * * * * * * * * * *");
 gotoxy(28,9);printf("1--Creat  File");
 gotoxy(28,10);printf("2--Delete  File");
 gotoxy(28,11);printf("3--OPen  File");
 gotoxy(28,12);printf("4--Write  File");
 gotoxy(28,13);printf("5--Locate  File");
 gotoxy(28,14);printf("6--Modify  File");
 gotoxy(28,15);printf("7--Copy  File");
 gotoxy(28,16);printf("8--Move  File");
 gotoxy(28,17);printf("9--Cataloge Manage");
 gotoxy(28,18);printf("10--Exit  File");
 gotoxy(25,21);
 printf("Please Choice:");
  scanf("%d",&i);
 return(i);  /*選擇相應的序號,相應的操作*/
}

main()
{int x,i,j,flag=1;
 char name[15],name1[15],name2[40];
 char choice,ch;
 int handle,status;  /*定義檔案的指標和狀態*/
 FILE *fp;
 while(flag)  /*初始化介面*/
{i=init();
 getchar();
 switch(i)
  { case 1:label1:  /*建立檔案操作*/
  clrscr();
  gotoxy(27,5);printf("CREAT  FILEn");
  for(j=0;j<40;j++)
  printf("= ");
  printf("nnPlease input the creating file name and routine:n");
  scanf("%s",name);
  getchar();
  handle=creatnew(name,0);/*按指定的檔案方式建立檔案,若有同名檔案返回錯誤程式碼*/
  if(handle==-1)
  { printf("nSorry,the file already exists.");
  getchar();
  printf("nInput again?(Y or N)");
  scanf("%c",&choice);getchar();
  if(choice=='Y'||choice=='y')
  goto label1;
  }
  else { printf("nThe file is created.");
  printf("Do you now input contentof the file?(Y or N):");
  while(1)  /*輸入建立檔案的內容*/ 
  { scanf("%c",&choice);
  if(choice=='y'||choice=='n'||choice=='Y'||choice=='N')
  break;
  else
  printf("nError!Please input again!");
  }
  if(choice=='y'||choice=='Y')
  { printf("nNow input content to the file(End with '#'):nn");
  fp=fopen(name,'w');/*把內容存放到fp指向的檔案中去*/
  ch=getchar();
  while(ch!='#')
  { fputc(ch,fp);
  ch=getchar();
  }
 fclose(fp);getchar();/*關閉檔案*/
  }
  getchar();
  break;
  case 2:label2:  /*刪除檔案的操作*/
  clrscr();
  gotoxy(25,5);printf("DELETE  FILEn");
  for(j=0;j<40;j++)
  printf("= ");
  printf("nnPlease input the deleting file name and routine:n");
  scanf("%s",name);  /*輸入要刪除的檔名*/
  getchar();
  printf("n Are you sure?(Y or N):");
  while(1)
  { scanf("%c",&choice);
  if(choice=='y'||choice=='n'||choice=='Y'||choice=='N')
  break;
  else
  printf("nError!Please input again!");
  }
  if(choice=='y'||choice=='Y')
  {status=access(name,0);/*獲取檔案的狀態,是否存在*/
  if(status!=0)
  {printf("nSorry the file doesn't exist!");
  getchar();
  printf("nnInput again?(Y or N)");
  scanf("%c",&choice);getchar();
  if(choice=='Y'||choice=='y')
  goto label2;
  }
  else
  {  status=access(name,02);/*獲取檔案的狀態,是否存在並且是否只讀*/
  if(status!=0)
 { printf("nSorry the file is only read!");
  getchar();
 }
  else
  {unlink(name);  /*從目錄中刪除一個檔案函式,該函式在dos.h中*/ 
 printf("nndelete succefully!");
 getchar();
  }
  }
  }
  getchar();
  break;
  case 3:label3:  /*開啟檔案操作*/ 
  clrscr();
  gotoxy(27,5);printf("OPEN  FILEn");
  for(j=0;j<40;j++)
  printf("= ");
  printf("nnPlease input the opening file name and routine:n");
  scanf("%s",name);
  status=access(name,0);/*獲取檔案的狀態*/
  if(status!=0)
  {printf("nSorry the file doesn't exist!");
  getchar();
  printf("nnInput again?(Y or N)");
  scanf("%c",&choice);getchar();
  if(choice=='Y'||choice=='y')
  goto label3;
  }
  else
  { printf("nNow begin to read the file:n");
    fp=fopen(name,'r');
  ch=fgetc(fp);  /*讀出檔案到*/
  while(ch!=EOF)
  {printf("%c",ch);
  ch=fgetc(fp);j++;
  }
  fclose(fp);getchar();/*關閉檔案*/
  }
  getchar();
  break;
  case 4:label4:  /*寫檔案操作*/
  clrscr();
  gotoxy(27,5);printf("WRITE  FILEn");
  for(j=0;j<40;j++)
  printf("= ");
  printf("nnPlease input the writing file name and routine:n");
  scanf("%s",name);
  status=access(name,0);/*獲取name指向的檔案狀態*/
  if(status!=0)
  {printf("nSorry the file doesn't exist!");
  getchar();
  printf("nnInput again?(Y or N)");
 scanf("%c",&choice);getchar();
  if(choice=='Y'||choice=='y')
 goto label4;
  }
  else
  {fp=fopen(name,'w');/*以寫入方式開啟name 指向的檔案*/
  printf("nPlease input the information(end with '#'):n");
  ch=getchar();  /*重寫檔案*/
  while(ch!='#')
  { fputc(ch,fp);
  ch=getchar();
  }
  fclose(fp);getchar();/*關閉檔案*/
  }
  getchar();
  break;
  case 5:label5:  /*定位檔案操作*/
  clrscr();
  gotoxy(27,5);printf("LOCATE  FILEn");
  for(j=0;j<40;j++)
  printf("= ");
  printf("nnPlease input the locating file name and routine:n");
  scanf("%s",name);
  status=access(name,0);/*獲取name檔案指向的檔案的狀態*/
  if(status!=0)
  {printf("nSorry the file doesn't exist!");
  getchar();
  printf("nnInput again?(Y or N)");
  scanf("%c",&choice);getchar();
  if(choice=='Y'||choice=='y')
  goto label5;
  }
  else
  {printf("nPlease input the location:");
  scanf("%d",&x);
  handle=open(name,O_CREAT|O_RDWR,S_IREAD|S_IWRITE);/*開啟由name指定的檔案,name既可以是簡單的檔名*/
  /*也可以是檔案的路徑名,O_CREAT表示了開啟檔案的存取程式碼,若檔案不存在,則建立,否則無效。*/
  /*O_RDWR表示開啟檔案用於讀寫。S_IREAD|S_IWRITE允許讀寫*/
  lseek(handle,x,SEEK_SET);/*該函式把由handle指定的檔案的檔案指標,移到SEEK_SET(開始位置)再加上x偏移量的地方*/
  getchar();
  }
  getchar();
  break;
  case 6:label6:  /*修改檔案屬性操作*/
  clrscr();
  gotoxy(27,5);printf("MODIFY  FILEn");
  for(j=0;j<80;j++)
  printf("= ");
  printf("nnPlease input the modifying attribution file name and routine:n");
  scanf("%s",name);
  status=access(name,0);/*獲取檔案的狀態*/
  if(status!=0)
  {printf("nSorry the file doesn't exist!");
  getchar();
  printf("nnInput again?(Y or N)");
  scanf("%c",&choice);getchar();
  if(choice=='Y'||choice=='y')
  goto label6;
  }
  else
  { printf("nPlease choice:1--READ_ONLY  2--WRITE_ONLY");
   printf("nnPlease choice the attributione operation:");
  while(1)
  { scanf("%d",&x);
  if(x==1||x==2)
  break;
  else
  printf("nError!Please input again!");
  }
  if(x==1) { status=chmod(name,S_IREAD);/*修改檔案為“只讀”*/
  if(status)
  printf("nSorry!Couldn't make the file read_only!");
  else
  printf("n===Made read_only===",name);
  getchar();
  }
  else if(x==2)  /*修改檔案為“只寫”*/
  { status=chmod(name,S_IWRITE);
  if(status)
  printf("nSorry!Couldn't make the file write_only!");
  else
  printf("n===Made write_only===",name);
  getchar();
  }
  }
  getchar();
  break;
  case 7:clrscr();  /*複製檔案的操作*/
  gotoxy(27,5);printf("COPY  FILEn");
  for(j=0;j<40;j++)
  printf("= ");
  printf("nnPlease input the copying file name and routine:n");
  scanf("%s",name);
  getchar();
  printf("nPlease input the copyed file name and routine:n");
  scanf("%s",name1);
  getchar();
  strcpy(name2,"copy ");
  strcat(name2,name);
  strcat(name2," ");
  strcat(name2,name1);
  system(name2);  /*系統dos指令*/
  getchar();
  break;
  case 8:clrscr();  /*移動檔案操作*/
  gotoxy(27,5);printf("MOVE  FILEn");
  for(j=0;j<40;j++)
  printf("= ");
  printf("nnPlease input the moving file name and routine:n");
  scanf("%s",name);
  getchar();
  printf("nPlease input the moving file name and routine:n");
  scanf("%s",name1);
  getchar();
  strcpy(name2,"move ");
  strcat(name2,name);
  strcat(name2," ");
  strcat(name2,name1);
  system(name2);  /*系統呼叫dos指令*/
  getchar();
  break;
  case 9: label9:  /*目錄管理操作*/
  clrscr();
  gotoxy(27,5);printf("CATALOGUE  MANAGEn");
  for(j=0;j<40;j++)
  printf("= ");
  gotoxy(13,9);
  printf("Please input the moving file name and routine:n");
  gotoxy(25,11);printf("1--display catalogue");
  gotoxy(25,12);printf("2--creat catalogue");
  gotoxy(25,13);printf("3--detele catalogue");
  gotoxy(25,14);printf("4--copy catalogue");
  gotoxy(25,15);printf("5--move catalogue");
  gotoxy(25,16);printf("6--exit catalogue");
  gotoxy(26,20);
  printf("Please choice:");
  scanf("%d",&x);
  while(x<1||x>6)
  {printf("nError!Please input again!n");
  scanf("%d",&x);
  }
  switch(x)
  { case 1: printf("nPlease iuput the displaying catalogue:n");
  scanf("%s",name);/*縣是目錄操作*/
  strcpy(name2,"dir ");/*複製dir命令*/
  strcat(name2, name);
  printf("%s",name2);
  getchar();
  system(name2);/*系統呼叫*/
  getchar();
  break;
  case 2: printf("nPlease iuput the creating catalogue:n");
  scanf("%s",name);/*建立目錄操作*/
  strcpy(name2,"md  ");/*複製md命令*/
  strcat(name2,name);
  system(name2);/*系統呼叫*/
  getchar();
  break;
  case 3: printf("nPlease iuput the deleting catalogue:n");
  scanf("%s",name);/*刪除目錄操作*/
  strcpy(name2,"rd  ");/*複製rd命令*/
  strcat(name2,name);
  system(name2);
  getchar();
  break;
  case 4: printf("nPlease iuput the copying catalogue:n");
  scanf("%s",name);/*複製目錄操作*/
  printf("nPlease iuput the displayed catalogue:n");
  scanf("%s",name1);
  strcpy(name2,"xcopy ");/*複製xcopy命令*/
  strcat(name2,name);
  strcat(name2," ");
  strcat(name2,name1);
  strcat(name2,"/e");
  system(name2);/*系統呼叫*/
  getchar();break;
  case 5: printf("nPlease iuput the moving catalogue:n");
  scanf("%s",name);/*移動目錄操作*/
  printf("nPlease iuput the moved catalogue:n");
  scanf("%s",name1);
  strcpy(name2,"move  ");/*複製move命令*/
  strcat(name2,name);
  strcat(name2," ");
  strcat(name2,name1);
  system(name2);
  getchar();break;
  case 6:goto tag;  /*退出目錄管理操作*/
  }
 printf("Input again?(Y or N)");
  scanf("%c",&choice);getchar();
 if(choice=='Y'||choice=='y')
  goto label9; 
 tag:getchar();
  break;
case 10:flag=0;exit(0);break;  /*退出系統*/
default: clrscr();
  printf("nn  Error!Please input again!n");
  getchar();
  break;
  }
  }
 }
}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-993857/,如需轉載,請註明出處,否則將追究法律責任。

相關文章