/**********
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. MFC打开文件对话框

    { CString FilePathName; CFileDialog dlg(TRUE);///TRUE为OPEN对话框,FALSE为SAVE AS对话框 if(dlg.DoModal()==IDO ...

  2. 数据库相关文章转载(1) MySQL性能优化之参数配置

    1.目的: 通过根据服务器目前状况,修改Mysql的系统参数,达到合理利用服务器现有资源,最大合理的提高MySQL性能. 2.服务器参数: 32G内存.4个CPU,每个CPU 8核. 3.MySQL目 ...

  3. counting sort 计数排序

    //counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorit ...

  4. pad 横屏 cell不正常显示

    在iOS9中,适配iPad横屏的时候,我发现cell不能正常显示,其标题和线都不是从左边头部开始,而是在中间,accessoryType的图标也不再右边尾部,效果如下图 但是在iPhone中是正常的, ...

  5. Request对象的主要方法

    setAttribute(String name,Object):设置名字为name的request的參数值 getAttribute(String name):返回由name指定的属性值 getAt ...

  6. Bootstrap 教程

    Bootstrap,来自 Twitter,是基于 HTML.CSS.JAVASCRIPT 的简介灵活的流行前段框架及交互组件集. 内容列表 Bootstrap 教程 Bootstrap 教程 Boot ...

  7. iOS开发——多线程OC篇&(十一)多线程NSOperation高级用法

    自定义NSOperation 一.实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UITableViewC ...

  8. How to Copy and Paste in the Ubuntu Gnome Terminal

    How to Copy: Select the content in terminal use your mouse , and then use Ctrl + Shift + C to copy t ...

  9. xy

    用C#實現Blob數據類型 PowerBuilder提供了Blob數據類型, 用來處理二進制數據(如:圖像,文件,二進制流,文本等等). 本質上, Blob是可變長的字節數組, 它提供了一系列全局函數 ...

  10. [设计模式3]--工厂(Factory)模式

    原文出处:http://blog.csdn.net/lwbeyond/article/details/7528309 工厂模式属于创建型模式,大致可以分为三类,简单工厂模式.工厂方法模式.抽象工厂模式 ...