linux 文件操作系统 设计

linux操作系统 文件管理系统设计

版本1.0

 

#include<fcntl.h> 

#include<sys/types.h> 

#include<sys/stat.h> 

#include<unistd.h> 

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<dirent.h> 



#define LENGTH 2000 

char str[100];

char buf[100];

void z_read(char* s); 

void z_write(char* s); 

void z_welcome();

void z_delete_file(char* s);

void z_delete_dir(char* s);

void z_create_file(char* s);

void z_create_dir(char *s);

void scan_dir(char* dir,int depth);



int main(){

	int in;

	do{

		z_welcome();

		scanf("%d",&in);

		switch(in){

			case 0: 

				exit(0);

			break;

			case 1: 

				printf("输入要创建的文件夹:");

				scanf("%s",&str);

				z_create_dir(str);

			break;

			case 2: 

				printf("输入要创建的文件:");

				scanf("%s",&str);

				z_create_file(str);

			break;

			case 3: 

				printf("输入要删除的文件夹:");

				scanf("%s",&str);

				z_delete_dir(str);

			break;

			case 4:

 				printf("输入要删除的文件:");

				scanf("%s",&str);

				z_delete_file(str);

			break;

			case 5:

 				printf("输入要写入文件的路径:");

				scanf("%s",&str);

				z_write(str);

			break;

			case 6: 

 				printf("输入要读取文件的路径:");

				scanf("%s",&str);

				z_read(str);

			break;

			case 7: 

				printf("输入要查找显示的路径:");

				scanf("%s",&str);

				scan_dir(str,0);

			break;	

			case 8:

				printf("输入要打开的路径:");

				scanf("%s",&str);

				chdir(str); 			

			break;	

			case 9:



				printf("%s\n",getcwd(buf,sizeof(buf)));

		}

	}while(1);

	return 0;

}

void z_welcome(){

	printf("|-->        welcome to zxl         <--|\n");

	printf("|-------------------------------------|\n");

	printf("|-->input 0 exit                    --|\n");

	printf("|-->input 1 to create work path     --|\n");

	printf("|-->input 2 to create a new folder  --|\n");

	printf("|-->input 3 to delete work path     --|\n");

	printf("|-->input 4 to delete a new folder  --|\n");

	printf("|-->input 5 to write folder         --|\n");

	printf("|-->input 6 to read folder          --|\n");

	printf("|-->input 7 to search path          --|\n");

	printf("|-->input 8 to open path            --|\n");

	printf("|-->input 9 to display into path    --|\n");

	printf("|-------------------------------------|\n");

}

void z_create_dir(char* s){

	if(mkdir(s,0755)!=-1){ 

		puts("目录创建成功");

	}else

		return 1;

}



void z_create_file(char* s){

	if(creat(s,0664)!=-1){ 

		puts("文件创建成功");

		//sleep(2);

	}

	else

		return 1;

}



void z_delete_dir(char* s){

	if(rmdir(s) !=-1){ 

		puts("删除目录成功");

	}

	else

		return 1;

}





void z_delete_file(char* s){

	if(unlink(s)!=-1){  

		puts("删除文件成功");

		//sleep(2);

	}

	else

		return 1;

}



void z_read(char* s){

	char c[LENGTH]; 

	int f,i,j=0; 

	f=open(s,O_RDONLY,LENGTH); 

	if(f!=-1){ 

		i=read(f,c,LENGTH);

		if(i>0){  

			for(;i>0;i--)

				putchar(c[j++]);

		}else

			perror("读取");	 

	}else

		perror("打开文件");	

}



void z_write(char* s){

	char c[LENGTH]; 

	int f,i;

	puts("输入要保存的文件信息:");

	if((i=read(0,c,LENGTH))<1){ 

		perror("读取失败");

		return 1 ;

	}

	f=open(s,O_RDWR | O_CREAT |O_APPEND ,0664);

	if(f!=-1){

		if(write(f,c,i)!=i) 

			perror("写入失败");

		puts("保存成功");

		close(f);	 

	}else

		perror("打开文件");

}



void scan_dir(char* dir,int depth) { 



DIR  *dp; 

struct dirent *entry;

struct stat statbuf; 

	if((dp=opendir(dir))==NULL){  

		puts("无法打开该目录");

		return;

	}

	chdir(dir);  

	while((entry = readdir(dp))!=NULL){ 

		lstat(entry->d_name,&statbuf);

		if(S_IFDIR & statbuf.st_mode){

			if(strcmp(".",entry->d_name)==0 || strcmp("..",entry->d_name)==0)

				continue; 

			printf("%*s%s/\n",depth,"",entry->d_name);

			scan_dir(entry->d_name,depth+4);

		}else

			printf("%*s%s\n",depth,"",entry->d_name); 

	}

	chdir(".."); 

	closedir(dp); 

}

 

版本2.0

 

#include<fcntl.h> 

#include<sys/types.h> 

#include<sys/stat.h> 

#include<unistd.h> 

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<dirent.h>





#define LENGTH 2000 

char str[100];

char buf[100];

char zz[]="find 		-name 		";

char z_file[50];

void z_read(char* s); 

void z_write(char* s); 

void z_welcome();

void z_delete_file(char* s);

void z_delete_dir(char* s);

void z_create_file(char* s);

void z_create_dir(char *s);

void scan_dir(char* dir,int depth);

void str_ins(char source[],int index,char dest[]);



int main(){

	int in;

	do{

		z_welcome();

		scanf("%d",&in);

		switch(in){

			case 0: 

				exit(0);

			break;

			case 1: 

				printf("输入要创建的文件夹:");

				scanf("%s",&str);

				z_create_dir(str);

			break;

			case 2: 

				printf("输入要创建的文件:");

				scanf("%s",&str);

				z_create_file(str);

			break;

			case 3: 

				printf("输入要删除的文件夹:");

				scanf("%s",&str);

				z_delete_dir(str);

			break;

			case 4:

 				printf("输入要删除的文件:");

				scanf("%s",&str);

				z_delete_file(str);

			break;

			case 5:

 				printf("输入要写入文件的路径:");

				scanf("%s",&str);

				z_write(str);

			break;

			case 6: 

 				printf("输入要读取文件的路径:");

				scanf("%s",&str);

				z_read(str);

			break;

			case 7: 

				printf("输入要查找显示的路径:");

				scanf("%s",&str);

				scan_dir(str,0);

			break;	

			case 8:

				printf("输入一个大致路径:");

				scanf("%s",&str);

				printf("输入查找的文件名:");

				scanf("%s",&z_file);

				str_ins(zz,5,str);

				str_ins(zz,strlen(zz),z_file);

				system(zz);

				printf("\n");

			break;	

			case 9:

				printf("输入要打开的路径:");

				scanf("%s",&str);

				chdir(str);

				break; 

			break;

			case 10:

				printf("当前路径:%s\n",getcwd(buf,sizeof(buf)));

				break;

		}

	}while(1);

	return 0;

}



void str_ins(char source[],int index,char dest[]){

	int i,j,k;

	j=0;while(dest[j])j++;j--;

	k=0;while(source[k])k++;k--;

	for(i=k-index+1;i>=0;i--) source[index+j+i]=source[index+i];

	for(i=0;i<=j;i++) source[i+index]=dest[i];

}





void z_welcome(){

	printf("|-->        welcome to zxl         <--|\n");

	printf("|-------------------------------------|\n");

	printf("|-->input 0 exit                    --|\n");

	printf("|-->input 1 to create work path     --|\n");

	printf("|-->input 2 to create a new folder  --|\n");

	printf("|-->input 3 to delete work path     --|\n");

	printf("|-->input 4 to delete a new folder  --|\n");

	printf("|-->input 5 to write folder         --|\n");

	printf("|-->input 6 to read folder          --|\n");

	printf("|-->input 7 to search path          --|\n");

	printf("|-->input 8 to search folder        --|\n");

	printf("|-->input 9 to open path            --|\n");

	printf("|-->input 10 to display into path   --|\n");

	printf("|-------------------------------------|\n");

}

void z_create_dir(char* s){

	if(mkdir(s,0755)!=-1){ 

		puts("目录创建成功");

	}else

		return 1;

}



void z_create_file(char* s){

	if(creat(s,0664)!=-1){ 

		puts("文件创建成功");

		//sleep(2);

	}

	else

		return 1;

}



void z_delete_dir(char* s){

	if(rmdir(s) !=-1){ 

		puts("删除目录成功");

	}

	else

		return 1;

}





void z_delete_file(char* s){

	if(unlink(s)!=-1){  

		puts("删除文件成功");

		//sleep(2);

	}

	else

		return 1;

}



void z_read(char* s){

	char c[LENGTH]; 

	int f,i,j=0; 

	f=open(s,O_RDONLY,LENGTH); 

	if(f!=-1){ 

		i=read(f,c,LENGTH);

		if(i>0){  

			for(;i>0;i--)

				putchar(c[j++]);

		}else

			perror("读取");	 

	}else

		perror("打开文件");	

}



void z_write(char* s){

	char c[LENGTH]; 

	int f,i;

	puts("输入要保存的文件信息:");

	if((i=read(0,c,LENGTH))<1){ 

		perror("读取失败");

		return 1 ;

	}

	f=open(s,O_RDWR | O_CREAT |O_APPEND ,0664);

	if(f!=-1){

		if(write(f,c,i)!=i) 

			perror("写入失败");

		puts("保存成功");

		close(f);	 

	}else

		perror("打开文件");

}



void scan_dir(char* dir,int depth) { 



DIR  *dp; 

struct dirent *entry;

struct stat statbuf; 

	if((dp=opendir(dir))==NULL){  

		puts("无法打开该目录");

		return;

	}

	chdir(dir);  

	while((entry = readdir(dp))!=NULL){ 

		lstat(entry->d_name,&statbuf);

		if(S_IFDIR & statbuf.st_mode){

			if(strcmp(".",entry->d_name)==0 || strcmp("..",entry->d_name)==0)

				continue; 

			printf("%*s%s/\n",depth,"",entry->d_name);

			scan_dir(entry->d_name,depth+4);

		}else

			printf("%*s%s\n",depth,"",entry->d_name); 

	}

	chdir(".."); 

	closedir(dp); 

}

 

 

 


截图:

linux 文件操作系统 设计

 

 

 

你可能感兴趣的:(linux)