zoukankan      html  css  js  c++  java
  • c实现 简单的文件管理 不含交互

    实现如下功能:


    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
    ");
        }
        else
        {
            printf("Create failure, change folders already exist
    ");
        }
    }
    
    /*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
    ",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)]='';
    
        fclose(fp);
        fp=NULL;
    
        int sum=KMP(str1,str2);
        if(sum==0)
        {
            printf("文件中不存在改字符串
    ");
        }
        else
        {
            printf("文件中存在该字符串,出现次数为:%d
    ",sum);
        }
    }
    
    /*8、把原文件移动到另一个位置(位置自定义),
    移动即不保留原文件。 */
    void move_file(char source[],char destination[])
    {
        CopyFile(source,destination,false);
        //remove(source);
        cout<<"移动成功
    ";
    }
    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("
    这是一个追加的内容","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;
    
    }
     
  • 相关阅读:
    Java线程中带有返回值的线程Callable
    Android 子线程测试
    JAVA下的Thread.sleep方法一定要try
    解决Android Studio 和 Android SDK Manager 无法在线更新的问题.
    asp.net关于页面不回发,不生成__doPostBack方法问题的完美解决方案
    git 使用2
    前端vue组件传参
    微信小程序 路由跳转 异步请求 存储数据,微信登录接口
    微信小程序 基本介绍及组件
    安装虚拟机
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9119631.html
Copyright © 2011-2022 走看看