1. 程式人生 > >c實現 簡單的文件管理 不含交互

c實現 簡單的文件管理 不含交互

字符 當前 str2 masm 器) 上層 stream file style

實現如下功能:


1、讀取指定目錄下的所有子目錄和文件信息(比如:指定目錄為C:/temp則把此目錄 下的所有子目錄下的文件信息讀出來)
2、在C盤創建一個以個人姓名命名的目錄(比如:張三)
3、在目錄下創建一個文件,並寫入自定義內容(比如文件名:abc.txt)
4、把上面所創建的文件復制到D盤
5、對上面復制到D盤的文件進行文件內容的追加
6、對D盤的文件實施改名操作
7、實現從文件中搜索是否存在用戶輸入的關鍵字信息(比如有文件abc.txt,接收用 戶輸入jdbc,查詢一下在abc.txt中是否存在以及有多少次出現jdbc相關文字,類似 於一個文件瀏覽器)
8、把原文件移動到另一個位置(位置自定義),移動即不保留原文件。
代碼如下:
#include <iostream>
#include <string.h>
#include <io.h>
#include <direct.h>
#include<sys/stat.h>
#include<sys/types.h>
#include <stdlib.h>
#include <windows.h>
#include<stdio.h>
#include <fstream>
using namespace std;

/*1、讀取指定目錄下的所有子目錄和文件信息(比如:指定目錄為C:/temp則把此目錄 下的所有子目錄下的文件信息讀出來)
*/ //深度優先遞歸遍歷當前目錄下文件夾和文件及子文件夾和文件 void DfsFolder(string path,int layer) { _finddata_t file_info; string current_path=path+"/*.*"; //也可以用/*來匹配所有 int handle=_findfirst(current_path.c_str(),&file_info); //返回值為-1則查找失敗 if(-1==handle) { cout<<"cannot match the path"<<endl;
return; } do { //判斷是否子目錄 if(file_info.attrib==_A_SUBDIR) { //遞歸遍歷子目錄 //打印記號反映出深度層次 for(int i=0; i<layer; i++) cout<<"--"; cout<<file_info.name<<endl; int layer_tmp=layer; if(strcmp(file_info.name,"..")!=0&&strcmp(file_info.name,".")!=0) //.是當前目錄,..是上層目錄,必須排除掉這兩種情況 DfsFolder(path+/+file_info.name,layer_tmp+1); //再windows下可以用\\轉義分隔符,不推薦 } else { //打印記號反映出深度層次 for(int i=0; i<layer; i++) cout<<"--"; cout<<file_info.name<<endl; } } while(!_findnext(handle,&file_info)); //返回0則遍歷完 //關閉文件句柄 _findclose(handle); } /*2.創建文件夾目錄*/ void create_folder(char szDirName[]) { bool flag = CreateDirectory(szDirName, NULL); DWORD err = GetLastError(); if(flag==1&&err==0) { printf("Create success\n"); } else { printf("Create failure, change folders already exist\n"); } } /*3、在目錄下創建一個文件,並寫入自定義內容(比如文件名:abc.txt)*/ int create_file_and_write_content(char s[],char path[]) { FILE*fp=NULL;//需要註意 fp=fopen(path,"w"); //創建文件 if(NULL==fp) return -1;//要返回錯誤代碼 fprintf(fp,"%s",s); //從控制臺中讀入並在文本輸出 fclose(fp); fp=NULL;//需要指向空,否則會指向原打開文件地址 } /*4、把上面所創建的文件復制到D盤*/ void copy_file(char source[],char destination[]) { CopyFile(source,destination,false);//覆蓋 } /*5、對上面復制到D盤的文件進行文件內容的追加*/ void file_content_addition(char str[],char path[]) { FILE*fp=NULL; fp=fopen(path,"a");// a 尾部追加數據 if(NULL==fp) return ; fprintf(fp,"%s",str); fclose(fp); fp=NULL; } /*6、對D盤的文件實施改名操作*/ void file_rename(char oldname[],char newname[]) { int result= rename( oldname, newname ); if ( result == 0 ) puts ( "File successfully renamed" ); else perror( "Error renaming file" ); } /*KMP*/ void getnext(char a[],int l,int next[]) { //a字符串數組為子串,l為字符串a的長度,next為a的匹配值數組 int j; int k=0; next[0]=0;//初始化 j=1; while(j<=l-1) { if(k==0)//a[0]和a[x]比較 { if(a[k]==a[j]) { k++;//k向後移動一位 next[j]=k; j++; } else { //k不動 next[j]=k; j++; } } if(k!=0)//k此時不在a[0]的位置上 { if(a[k]==a[j]) { k++;//k後移一位 next[j]=k; j++;//j後移一位 } else { k=0;//k重新回到a[0] } } } } int KMP(char str[],char a[]) { int L=strlen(str);//字符串長度 int l=strlen(a); int i,j; i=j=0; int next[l]; getnext(a,l,next);//活動匹配值數組 int sum=0;//匹配成功的次數 while(i<=L&&j<=l) { if(str[i]==a[j]&&j==0)//匹配中的四種情況 { i++; j++; } else if(str[i]==a[j]&&j!=0) { i++; j++; } else if(str[i]!=a[j]&&j==0) { j=0; i++; } else if(str[i]!=a[j]&&j!=0) { int s=j-next[j-1]; i=i-j+s; j=0; } if(j==l)//匹配成功的條件 { //printf("第%d此成功匹配的位置為:%d\n",sum,i-l); sum++; } } return sum; } /* 7、實現從文件中搜索是否存在用戶輸入的關鍵字信息 (比如有文件abc.txt,接收用 戶輸入jdbc, 查詢一下在abc.txt中是否存在 以及有多少次出現jdbc相關文字,類似 於一個文件瀏覽器) ps:采用了KMP算法優化 */ int count_str_appear_num(char path[],char str2[]) { FILE*fp=NULL; fp=fopen(path,"r");// if(NULL==fp) return 0; char str1[1024]; fscanf(fp,"%s",str1); str1[strlen(str1)]=\0; fclose(fp); fp=NULL; int sum=KMP(str1,str2); if(sum==0) { printf("文件中不存在改字符串\n"); } else { printf("文件中存在該字符串,出現次數為:%d\n",sum); } } /*8、把原文件移動到另一個位置(位置自定義), 移動即不保留原文件。 */ void move_file(char source[],char destination[]) { CopyFile(source,destination,false); //remove(source); cout<<"移動成功\n"; } int main(int argc,char *argv[]) { //遞歸遍歷文件夾 //DfsFolder("D:\masm",0); // create_folder("D:\\masm\\test"); // create_file_and_write_content("這是一個寫入的內容","D:\\masm\\test1.txt"); //copy_file("D:\\masm\\test1.txt","D:\\test2.txt"); //file_content_addition("\n這是一個追加的內容","D:\\test2.txt"); // file_rename("D:\\test2.txt","D:\\test888.txt"); //count_str_appear_num("D:\\masm\\test1.txt","abc"); // move_file("D:\\masm\\test1.txt","D:\\test1.txt"); return 0; }

c實現 簡單的文件管理 不含交互