?? 文件管理.cpp
字號:
#include<stdio.h>
#include <stdlib.h>
#include <conio.h>
#include<string.h>
struct ufd{//文件主目錄塊
char filename[10];
char password[10];
char filelength[10];
struct ufd *next;
}*file,*p,*pr;
struct mdf{//用戶目錄塊
char username[10];
struct ufd *link;
}user[10]={{"one",NULL},{"two",NULL},{"three",NULL},{"four",NULL},
{"five",NULL},{"six",NULL},{"seven",NULL},{"eight",NULL},
{"nine",NULL},{"ten",NULL}};
struct afd{//打開目錄塊
char openname[10];
char openpassword[10];
struct afd *next;
}*open,*openpr;
char *cmd;
void initfile(){
int i;
p=(ufd*)malloc(sizeof(ufd));//user1
strcpy(p->filename,"user1");
strcpy(p->password,"123");
strcpy(p->filelength,"20");
p->next=NULL;
pr=p;
file=pr;
//user2
p=(ufd*)malloc(sizeof(ufd));
strcpy(p->filename,"user2");
strcpy(p->password,"123");
strcpy(p->filelength,"20");
p->next=NULL;
pr->next=p;
pr=pr->next;
//user3
p=(ufd*)malloc(sizeof(ufd));
strcpy(p->filename,"user3");
strcpy(p->password,"123");
strcpy(p->filelength,"20");
p->next=NULL;
pr->next=p;
}
void createfile(){
ufd *q;
int n=1;
for(pr=file;pr->next!=NULL;pr=pr->next) n++;
if(n!=10){
p=(ufd*)malloc(sizeof(ufd));
printf("\n請輸入要創(chuàng)建的文件名:");
scanf("%s",p->filename);
q=file;
while(q!=NULL){
for(q=file;q!=NULL;q=q->next){
if(strcmp(q->filename,p->filename)) continue;
if(!strcmp(q->filename,p->filename)){
printf("\n已存在該文件名,請重新輸入要創(chuàng)建的文件名:");
scanf("%s",p->filename);
q=file;
continue;
}
}
}
printf("\n請輸入要創(chuàng)建的文件保護碼:");
scanf("%s",p->password);
strcpy(p->filelength,"20");
p->next=NULL;
pr->next=p;
printf("\n文件創(chuàng)建成功!\n");
}
else{
printf("\n文件個數(shù)超出10個!不允許再創(chuàng)建!\n");
}
}
void deletefile(){
ufd *q;
q=(ufd*)malloc(sizeof(ufd));
int n=0;
if(file!=NULL){
printf("\n請輸入要刪除的文件名:");
scanf("%s",q->filename);
if(!strcmp(q->filename,file->filename)){
file=file->next;
n=1;
}
else{
for(pr=file;pr->next!=NULL;){
if(!strcmp(q->filename,pr->next->filename)){
pr->next=pr->next->next;
n=1;
continue;
}
pr=pr->next;
}
}
if(n==1) printf("\n刪除成功\n");
if(n==0) printf("\n不存在該文件名\n");
}
else printf("\n當前用戶的文件已全部被刪除!\n");
}
void outputopen(){//輸出打開文件目錄
printf("\n已打開文件目錄如下:\n");
printf("\n 文件名 保護碼\n");
for(openpr=open;openpr!=NULL;openpr=openpr->next){
printf("\n %-11s ",openpr->openname);
printf(" %-11s \n",openpr->openpassword);
}
}
void openfile(){//打開文件程序
afd *q;
int i=0;
q=(afd*)malloc(sizeof(afd));
q->next=NULL;
printf("\n請輸入要打開的文件名:");
scanf("%s",q->openname);
for(pr=file;pr!=NULL;pr=pr->next){
if(!strcmp(q->openname,pr->filename)){
i=1;
break;
}
}
if(open!=NULL){
for(openpr=open;openpr!=NULL;openpr=openpr->next){
if(!strcmp(q->openname,openpr->openname)) i=2;
}
}
if(i==1){
printf("\n請輸入要打開文件的保護碼:");
scanf("%s",q->openpassword);
if(!strcmp(q->openpassword,pr->password)){
printf("\n已成功打開文件!\n");
if(open==NULL) open=q;
else{
for(openpr=open;openpr->next!=NULL;openpr=openpr->next);
openpr->next=q;
}
outputopen();
}
else{
printf("\n保護碼輸入錯誤!\n");
}
}
else if(i==2) printf("\n該文件已經(jīng)打開!\n");
else printf("\n沒有你要打開的文件或已被刪除!\n");
}
void closefile(){
afd *q;
int count=0;
q=(afd*)malloc(sizeof(afd));
if(open==NULL) printf("\n沒有打開的文件!\n");
else{
printf("\n請輸入要關閉的文件名:");
scanf("%s",q->openname);
if(!strcmp(q->openname,open->openname)){
open=open->next;
count=1;
printf("\n該文件已經(jīng)成功關閉!\n");
}
else{
for(openpr=open;openpr->next!=NULL;){
if(!strcmp(q->openname,openpr->next->openname)){
openpr->next=openpr->next->next;
count=1;
printf("\n該文件已經(jīng)成功關閉!\n");
}
else openpr=openpr->next;
}
}
if(count==0) printf("\n沒有打開該文件或不存在該文件!\n");
if(open==NULL) printf("\n所有文件都已經(jīng)關閉!\n");
if(open!=NULL) outputopen();
}
}
void readfile(){
afd *q;
int n;
q=(afd*)malloc(sizeof(afd));
if(open==NULL) printf("\n沒有打開文件!不能讀,請先打開文件!\n");
else{
printf("\n請輸入要閱讀的文件名:");
scanf("%s",q->openname);
for(openpr=open;openpr!=NULL;openpr=openpr->next){
if(!strcmp(q->openname,openpr->openname)) n=2;
}
if(n==2){
for(pr=file;pr!=NULL;pr=pr->next){
if(!strcmp(q->openname,pr->filename)){
printf("\n 文件名 保護碼 文件長度 \t\n");
printf("\n %-11s ",pr->filename);
printf(" %-11s ",pr->password);
printf(" %-11s\n",pr->filelength);
}
}
}
else{
printf("\n沒有打開該文件!不能讀,請先打開該文件!\n");
}
}
}
void writefile(){
afd *q;
int n,len;
q=(afd*)malloc(sizeof(afd));
if(open==NULL) printf("\n沒有打開文件!不能寫,請先打開文件!\n");
else{
printf("\n請輸入要寫入的文件名:");
scanf("%s",q->openname);
for(openpr=open;openpr!=NULL;openpr=openpr->next){
if(!strcmp(q->openname,openpr->openname)) n=2;
}
if(n==2){
for(pr=file;pr!=NULL;pr=pr->next){
if(!strcmp(q->openname,pr->filename)){
printf("\n 文件名 保護碼 文件長度 \t\n");
printf("\n %-11s ",pr->filename);
printf(" %-11s ",pr->password);
printf(" %-11s\n",pr->filelength);
printf("\n請輸入寫入文件長度:");
scanf("%s",pr->filelength);
printf("\n寫入成功!\n");
printf("\n 文件名 保護碼 文件長度 \t\n");
printf("\n %-11s ",pr->filename);
printf(" %-11s ",pr->password);
printf(" %-11s\n",pr->filelength);
}
}
}
else{
printf("\n沒有打開該文件!不能讀,請先打開該文件!\n");
}
}
}
void outputfile(){
printf("\n 文件名 保護碼 文件長度 \t\n");
for(p=file;p!=NULL;p=p->next){
printf("\n %-11s ",p->filename);
printf(" %-11s ",p->password);
printf(" %-11s\n",p->filelength);
}
}
void admin(){
int i;
cmd=(char*)malloc(sizeof(char));
printf("\n請輸入用戶名:");
scanf("%s",cmd);
for(i=0;;i++){
for(i=0;i<10;i++){
if(!strcmp(cmd,user[i].username)) break;
}
if(i==10){
printf("\n無此用戶文件!\n\n");
printf("請輸入用戶名:");
scanf("%s",cmd);
continue;
}
break;
}
}
main(){
int n=0;
admin();
initfile();
printf("\n當前用戶文件目錄如下:\n");
outputfile();
while(strcmp(cmd,"bye")){
printf("\n請輸入操作命令:");
scanf("%s",cmd);
if(!strcmp(cmd,"create")){
createfile();
continue;
}
if(!strcmp(cmd,"delete")){
deletefile();
continue;
}
if(!strcmp(cmd,"open")){
for(openpr=open;openpr!=NULL;openpr=openpr->next) n++;
if(n==5)printf("\n打開的文件已經(jīng)達到5個,不允許再打開文件!\n");
else openfile();
n=0;
continue;
}
if(!strcmp(cmd,"close")){
closefile();
continue;
}
if(!strcmp(cmd,"read")){
readfile();
continue;
}
if(!strcmp(cmd,"write")){
writefile();
continue;
}
if(!strcmp(cmd,"bye"));
else printf("\n無此操作命令!\n");
}
if(file==NULL) printf("\n當前用戶沒有保存任何文件!\n\n");
else{
printf("\n當前用戶保存的文件目錄如下:\n");
outputfile();
}
printf("\n");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -