c语言实现自定义命令对文件进行相关操作

c语言实现自定义命令对文件进行相关操作

期末实践作业

#include <windows.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment (lib,"User32.lib")

#include <dirent.h> 
#include <direct.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#define MAX_LINE 1024
//
//获取系统时间
void gettime()
{
	int sec, min,hour,mday,mon,year,wday;//参数定义
	struct tm * tmptr;
	time_t secnow;
	time(&secnow);//获取系统时间
	tmptr=localtime(&secnow);//转化为当地时间
	year=tmptr->tm_year;//赋值给相应变量
	mon=tmptr->tm_mon;
	mday=tmptr->tm_mday;
	wday=tmptr->tm_wday;
	hour=tmptr->tm_hour;
	min=tmptr->tm_min;
	sec=tmptr->tm_sec;
	printf("%4d-%02d-%02d %02d:%02d:%02d\n",1900+year,1+mon,mday,hour,min,sec);
}


//回显字符串

void review_num_string()
{
	char a[100];
	printf("请输入字符串\n");
	scanf("%s",a);
	printf("%s\n",a);
}


//创建目录
int create_list()
{
	char a[30];							//在当前目录下创建子目录
	LPSTR szDirPath;					//注意再次新建的时候由于之前已经成功新建,就会提示创建失败
	printf("请输入要创建目录的名称:\n");
	scanf("%s",a);
	szDirPath=a;
	if(!CreateDirectory(szDirPath,NULL))
	{
		printf("创建目录%s失败\n",szDirPath);
		return 0;
	}
	printf("创建目录%s成功\n",szDirPath);

	return 1;
}

//
//显示当前目录
int display_list()
{
	CHAR szCurrentDirectory[MAX_PATH];//用于储存当前路径
	DWORD dwCurDirPathlen;//当前路径的长度,也用于判断获取是否成功
	dwCurDirPathlen=GetCurrentDirectory(MAX_PATH,szCurrentDirectory);//获取当前路径
	if(dwCurDirPathlen==0)
	{	
		printf("获取当前目录失误。\n");
		return 0;
	}
	printf("进程当前目录为%s\n",szCurrentDirectory);
	return 1;
}




//
//删除目录,也就是删除文件夹
void delete_dir()
{
	char str[20];
	CHAR szCurrentPath[MAX_PATH];
	GetCurrentDirectory(MAX_PATH,szCurrentPath);
	printf("请输入要删除目录名称\n");
	scanf("%s",str);
	strcat(szCurrentPath,"\\");
	strcat(szCurrentPath,str); 
	if(RemoveDirectory(szCurrentPath)==0)
	printf("删除目录%s失败!\n",szCurrentPath);
	else
	printf("删除目录%s成功!\n",szCurrentPath);
}


//
//展现当前目录下的所有文件 
void show_file()
{
	CHAR szCurrentDirectory[MAX_PATH];//用于储存当前路径
	DWORD dwCurDirPathlen;//当前路径的长度,也用于判断获取是否成功
	dwCurDirPathlen=GetCurrentDirectory(MAX_PATH,szCurrentDirectory);//获取当前路径  
	DIR *dir = NULL; 
	dir = opendir(szCurrentDirectory);
	struct dirent *entry;  
    while(entry=readdir(dir))  {  
	    printf("%s\n",entry->d_name);  //输出文件或者目录的名称   
    }  
    closedir(dir);    
}

//
//显示日期 
void getDate()
{
	int mday,mon,year;//参数定义
	struct tm * tmptr;
	time_t secnow;
	time(&secnow);//获取系统时间
	tmptr=localtime(&secnow);//转化为当地时间
	year=tmptr->tm_year;//赋值给相应变量
	mon=tmptr->tm_mon;
	mday=tmptr->tm_mday;
	printf("%4d-%02d-%02d\n",1900+year,1+mon,mday);
}

//
//删除文件 
void deleteFile()
{
	//找到当前目录 
	char str[20];
	char fileName[100];
	CHAR szCurrentDirectory[MAX_PATH];//用于储存当前路径
	GetCurrentDirectory(MAX_PATH,szCurrentDirectory);
	printf("请输入要删除的文件名\n");
	scanf("%s",fileName);
	DIR *dir = NULL; 
	dir = opendir(szCurrentDirectory);
	struct dirent *entry;  
    while(entry=readdir(dir)) { 
	 	if(stricmp(fileName,entry->d_name)==0){
	 	DeleteFile(fileName);
	    printf("删除成功");
	    return;
		}
	}  
    printf("删除失败,没有该文件");
    closedir(dir);    
}


//
//更改路径 
//void modify_path()
//{
//	char path[254];
//	char fileName[254];
//	struct stat buf;
//	printf("请输入要修改的文件名\n");
//	scanf("%s",fileName);
//	CHAR szCurrentDirectory[MAX_PATH];//用于储存当前路径
//	DWORD dwCurDirPathlen;//当前路径的长度,也用于判断获取是否成功
//	GetCurrentDirectory(MAX_PATH,szCurrentDirectory);
//	DIR *dir = NULL; 
//	dir = opendir(szCurrentDirectory);
//	struct dirent *entry;  
//    while(entry=readdir(dir)) { 
//   		
//	 	if(stricmp(fileName,entry->d_name)==0){
//	 		printf("请输入除了C盘以外的转移目录\n");
//	 		scanf("%s",path); 
//	 		stat(path,&buf);
//			//判断path路径是否存在
//			if(S_ISDIR(buf.st_mode)){  //如果是目录转移 
//				lstrcat(szCurrentDirectory,"\\");
//				lstrcat(path,"\\");	
//				lstrcat(szCurrentDirectory,fileName);
//				lstrcat(path,fileName);
//				//坑,复制到C盘会权限不够 
//				if(!CopyFile(szCurrentDirectory,path,TRUE)){
//				DWORD dw = GetLastError();
//				printf("失败%d",dw); 
//				closedir(dir);
//				}else{
//				DeleteFile(szCurrentDirectory);
//				printf("修改成功");
//				closedir(dir);
//				return ; 
//				} 
//			}else{   //是文件则更名 
//				rename(fileName,path); 
//				printf("修改成功");
//				return;
//			}  
//	
//		}
//	} 
//	printf("没有这个文件");
//	closedir(dir); 
//}


//
//更改路径 
void modify_path()
{
	char path[255];
	printf("请输入要修改的工作目录\n");
	scanf("%s",path);
	if(SetCurrentDirectory(path)){
		printf("修改成功\n");
		display_list();
	}else{
		printf("失败");
	}
	
}



//文件重命名 
void file_rename(){
	char oldFileName[254];
	char newFileName[254];
	printf("请输入要修改的旧文件名\n");
	scanf("%s",oldFileName);
	CHAR szCurrentDirectory[MAX_PATH];//用于储存当前路径
	GetCurrentDirectory(MAX_PATH,szCurrentDirectory);
	DIR *dir = NULL; 
	dir = opendir(szCurrentDirectory);
	dir = opendir(szCurrentDirectory);
	struct dirent *entry;  
    while(entry=readdir(dir)) { 
    
	if(stricmp(oldFileName,entry->d_name)==0){
		printf("请输入新文件名\n");
		scanf("%s",newFileName);
		rename(oldFileName,newFileName);
		printf("修改文件%s为%s成功",oldFileName,newFileName);
		closedir(dir); 
		return;
	}
} 
	printf("文件名输入有误");
	closedir(dir);
}


//清屏
void clearAll(){
	system("cls");
}



//当前目录的所有文件或目录和目录的子文件和目录 
void printfDir(const char* path,int depth)
{
	
	DIR *dir;
	struct dirent *entry;
	struct stat buf;
	dir = opendir(path);
	if(dir == NULL)
	{
	fprintf(stdout,"open this %s fail\n",path);
	return;
	}
	
	chdir(path);   //进入目录
	while((entry = readdir(dir)) != NULL)  //逐个读取目录总文件
	{
	stat(entry->d_name,&buf);   //得到文件的各种属性,保存在struct buf中
	
	if(S_ISDIR(buf.st_mode)){  //如果这个文件是目录
	if(strcmp(".",entry->d_name)==0 || strcmp("..",entry->d_name)==0)//去掉"." ".."这两个目录
	continue;
	fprintf(stdout,"%*s%s/\n",depth,"",entry->d_name); 
	printfDir(entry->d_name,depth+4);
	}
	fprintf(stdout,"%*s%s\n",depth,"",entry->d_name);
	}
	chdir("..");
	closedir(dir);
}


//输出到文件 ,打印文本命令 
void printfFile()
{
	char fileName[255];
	char content[255];
	FILE *file;
	
	printf("请输入要打印的文件\n");
	scanf("%s",fileName);
	
	file = fopen(fileName,"a+");
	printf("请输入要打印的内容\n");
	scanf("%s",content);
	fprintf(file,"%s\n",content);
	printf("完成\n");
	fclose(file);
}


//显示文本命令 
void printFileOnSc()
{
	char fileName[255];
    printf("请输入要显示内容的文件名\n");
    scanf("%s",fileName);
    char buf[MAX_LINE];  /*缓冲区*/
    FILE *fp;            /*文件指针*/
    int len;             /*行字符个数*/
	if((fp =fopen(fileName,"r")) == NULL)
	{
	perror("fail to read");	
	exit(1) ;	
	}
	while(fgets(buf,MAX_LINE,fp) != NULL)
	{
	buf[len-1] = '\0';  /*去掉换行符*/
	printf("%s",buf);
	}
}

//
//显示版本命令 
void showVersion()
{ 
	printf("版本信息:             \n");
	printf("version           1.0.0\n");
	printf("author            huidu\n");
}
	


//比较输入的字符串指令是否合法
int cmp_char()
{

	char ch[10];
	printf("\n请输入相应的操作指令:");
	scanf("%s",ch);
	if(stricmp(ch,"st")==0) return 1;
	else if(stricmp(ch,"rn")==0) return 2;
	else if(stricmp(ch,"nm")==0) return 3;
	else if(stricmp(ch,"cm")==0) return 4;
    else if(stricmp(ch,"esc")==0) return 5;
	else if(stricmp(ch,"pr")==0) return 6;
	else if(stricmp(ch,"ls")==0) return 7;
	else if(stricmp(ch,"sd")==0) return 8;
	else if(stricmp(ch,"rd")==0) return 9;
	else if(stricmp(ch,"rp")==0) return 10;
	else if(stricmp(ch,"rnf")==0) return 11;
	else if(stricmp(ch,"cc")==0) return 12;
	else if(stricmp(ch,"sdir")==0) return 13;
	else if(stricmp(ch,"wr")==0) return 14;
	else if(stricmp(ch,"sc")==0) return 15;
	else if(stricmp(ch,"sb")==0) return 16;
	else return 0;
}


int escs()
{
	return 0;
}
///
//命令解释器
void Command_interpreter()
{
	CHAR szCurrentDirectory[MAX_PATH];//用于储存当前路径
	DWORD dwCurDirPathlen;//当前路径的长度,也用于判断获取是否成功
	GetCurrentDirectory(MAX_PATH,szCurrentDirectory);
	int a;

	char ar[10];

	while(1)
	{
		//  ls   nm    st     cm    sd     rn     pr

		printf("\n*************************************************************************\n");
		printf("*  列目录命令   ls     |显示时间命令 st   |显示日期命令  sd   |回显字符串命令          rn   *\n");
		printf("*  创建目录命令 nm     |删除目录命令 cm   |更改路径命令  rp   |显示当前工作目录        pr   *\n");
		printf("*  删除文件命令 rd     |打印文本命令 wr   |文件重新命    rnf  |显示文本命令            sc   *\n");
		printf("*  显示版本命令 sb     |显示目录结构命令  sdir                |清除当前显示内容命令    cc   *\n");
		printf("\n*************************************************************************\n");
		

		a=cmp_char();

		switch(a)
		{ 
			case 1: gettime();break;
			case 2: review_num_string();break;
			case 3: create_list();break;
			case 4: delete_dir();break;
			case 5: exit(0);break;
			case 0: printf("无法识别的指令,请输入正确的指令!\n");break;
			case 6:display_list();break;
			case 7:show_file();break;
			case 8:getDate();break;
			case 9:deleteFile();break;
			case 10:modify_path();break;
			case 11:file_rename();break;
			case 12:clearAll();break;
			case 13:printfDir(szCurrentDirectory,0);break;
			case 14:printfFile();break;
			case 15:printFileOnSc();break;
			case 16:showVersion();break;
			default:break;
		}
	}
}

int main(int argc, char* argv[])
{
	int i=0,j=3;
	char d[10];

	printf("请输入cmd进入命令解释器:");
	scanf("%s",d);
	for(i=0;i<2;i++)
	{
		if(stricmp(d,"cmd")==0)
		{
			Command_interpreter();
		}
		else
		{
			j=j-1;
			printf("你还有%d次机会!",j);
			printf("请输入cmd进入命令解释器:");
			scanf("%s",d);
		}
	}
	printf("连续输入错误,退出程序!");
	return 0;
}

代码有很多不足,主要用来复习和参考相关函数示列使用!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值