/**********
huangsy13@gmail.com
FileStruct.h
**********/
#ifndef FILESTRUE
#define FILESTRUE
 
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include <sys/stat.h>
#include<dirent.h>
#include<vector>
#include<cstring>
#include<iostream>
#include <sstream>
#include <fstream>
 
using namespace std ;
 
string numToStr(int numn){
    stringstream ss;
    ss << numn;
    string s2;
    ss>>s2;
    return s2;
}
 
void tranFile( string inFile, string outFile ){
    fstream fsCopee( inFile.data(), ios::binary | ios::in ) ;
    fstream fsCoper( outFile.data(), ios::binary | ios::out ) ;
    fsCoper << fsCopee.rdbuf() ;
}
 
struct FileStruct{
    string path;
    std::vector<string> fileNames;
    std::vector<string> dirNames;
    FileStruct(){
    }
    FileStruct(string path0){
        setUp(path0);
    }
    void setUp(string path0){
        fileNames.clear();
        dirNames.clear();
        path = path0;
        DIR *dp;
        struct dirent *dirp;
        if((dp=opendir(path.data()))==NULL){
            return;
        }
        while((dirp=readdir(dp))!=NULL){
            if((strcmp(dirp->d_name,".")==0)||(strcmp(dirp->d_name,"..")==0))
                continue;
            struct stat st;
            string ss = path + "/" + dirp->d_name;
            stat(ss.data() , &st );
            if (S_ISDIR(st.st_mode))
                dirNames.push_back(dirp->d_name);
            else
                fileNames.push_back(dirp->d_name);
        }
        closedir(dp);
    }
    int getFileNum(){
        return fileNames.size();
    }
    int getDirNum(){
        return dirNames.size();
    }
    string getFullDirPath(int o){
        if (o >= getDirNum()){
            return "";
        }
        return path+"/"+dirNames[o];
    }
    string getFullFilePath(int o){
        if (o >= getFileNum()){
            return "";
        }
        return path+"/"+fileNames[o];
    }
    string getFullDirPath(string str){
        return path+"/"+str;
    }
    string getFullFilePath(string str){
        return path+"/"+str;
    }
    bool isDir(string sPath = ""){
        struct stat st;
        string ss;
        if (sPath == ""){
            ss = path;
        }
        else{
            ss = sPath;
        }
        stat(ss.data() , &st );
        if (S_ISDIR(st.st_mode))
            return true;
        else
            return false;
    }
    int getAllFileNum(){
        int fileNum = 0;
        for (int i = 0 ; i < getDirNum() ; i++){
            FileStruct childFile(getFullDirPath(i));
            fileNum += childFile.getAllFileNum();
        }
        return fileNum+getFileNum();
    }
    int getAllDirNum(){
        int dirNum = 0;
        for(int i = 0 ; i < getDirNum() ;i++){
            FileStruct childFile(getFullDirPath(i));
            dirNum += 1 + childFile.getAllDirNum();
        }
        return dirNum;
    }
};
 
#endif

C++之linux下文件结构实现的更多相关文章

  1. linux下的文件结构

    linux下的文件结构,看看每个文件夹都是干吗用的/bin 二进制可执行命令/dev 设备特殊文件/etc 系统管理和配置文件/etc/rc.d 启动的配置文件和脚本/home 用户主目录的基点,比如 ...

  2. Linux下的文件结构,及对应文件夹的作用

    Linux下的文件结构,及对应文件夹的作用 /bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基点,比 ...

  3. (转载)linux下各个文件夹的作用

    linux下的文件结构,看看每个文件夹都是干吗用的/bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基 ...

  4. Linux下的C编程实战

    Linux下的C编程实战(一) ――开发平台搭建 1.引言 Linux操作系统在服务器领域的应用和普及已经有较长的历史,这源于它的开源特点以及其超越Windows的安全性和稳定性.而近年来, Linu ...

  5. 细说Linux下软件包的安装与管理

    一 源码安装方式      由于linux操作系统开放源代码,因而在其上安装的软件大部分也都是开源软件,例如apache.tomcat.php等软件.开源软件基本都提供源码下载,源码安装的方式:源码安 ...

  6. linux下的文件系统

    转http://www.cnblogs.com/yyyyy5101/articles/1901842.html 谈谈个人对于文件系统的认识,其实这也体现了计算机操作系统的抽象:你不用管计算机中的文件如 ...

  7. Linux下的编程实战【转】

    一篇比较不错的文章, 降到了 makefile make , gcc编译器,GDB调试器, Linux文件系统,Linux文件API,.C语言库函数(C库函数的文件操作实际上是独立于具体的操作系统平台 ...

  8. 用C写一个web服务器(三) Linux下用GCC进行项目编译

    .container { margin-right: auto; margin-left: auto; padding-left: 15px; padding-right: 15px } .conta ...

  9. .Neter玩转Linux系列之三:Linux下的分区讲解

    基础篇 .Neter玩转Linux系列之一:初识Linux .Neter玩转Linux系列之二:Linux下的文件目录及文件目录的权限 .Neter玩转Linux系列之三:Linux下的分区讲解 .N ...

随机推荐

  1. 25.怎样创建一个Swift项目?

    经历前面三部分的学习之后,我们对于Swift的有了基本的了解,知道它的基础语法,也知道了类.结构体.枚举.协议.扩展等等内容.但知道上面这些内容,并不代表我们就能很好的进行实际的项目开发了,本部分内容 ...

  2. spring 切面 前置后置通知 环绕通知demo

    环绕通知: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...

  3. PHP抓取豆瓣读书爬虫代码

    <?php//演示地址 http://asizu.sinaapp.com/reptile_douban.php//数据量不是特别大,没有写抓完数据便停止. 喜欢的朋友拿去自己改改就好了 head ...

  4. js url图片转bese64

    function convertImgToDataURLviaCanvas(url, callback, outputFormat){ var img = new Image(); img.cross ...

  5. php 内置http服务器

    PHP从5.4.0起,内置了一个http服务器,开发人员可以借助这个内置服务器来做一些本地测试. 启动服务器: 打开终端,进入php安装目录,然后执行 php -S localhost: 这样就可以开 ...

  6. HTML第五天学习笔记

    今天先是学习了基础的css样式 <html> <head> <title></title> <meta http-equiv = "co ...

  7. DevExpress 13.2.6源码、安装包、汉化包下载和教程

    DevExpress比DotNetBar控件成熟很多,当然源码是公开的,但是最新版本需要9K多.如果不是土豪,用已经破解的版本或者自己拿源码编译一份就可以了,老外就是这么好. 首先在这里下载然后解压准 ...

  8. hdu 4739 Zhuge Liang's Mines 随机化

    Zhuge Liang's Mines Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.p ...

  9. Codeforces Round #310 (Div. 1) A. Case of Matryoshkas 水题

    C. String Manipulation 1.0 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  10. 【JavsScript】父子页面之间跨域通信的方法

    由于同源策略的限制,JavaScript跨域的问题,一直是一个比较棘手的问题,为了解决页面之间的跨域通信,大家煞费苦心,研究了各种跨域方案.之前也有小网同学分享过一篇“跨域,不再纠结” 开始照着尝试时 ...