/**********
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. 如何加入自定义WebControl

    http://www.screencast.com/users/Dennis.Garavsky/folders/Default/media/c75b4ec6-1641-4f82-936e-39360d ...

  2. CentOS FireFox Flash Player

    yum install *firefox* yum install flash-plugin

  3. [效果]JS折叠菜单-使用方法 (Moo.Fx)

    (从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-06-24) 用法: 1.添加JS库 CODE:<script src="prototype.lite.js& ...

  4. 【项目实例】使用C#开发纽曼USB来电通来电弹屏客户端小结

    基于CRM客户和咨询者的普遍需求,老板决定在CRM系统上加入来电弹屏功能,所谓来电弹屏,就是当一个电话打入时,电脑会弹出该电话号码对应的客户.联系人或者供应商详细信息,如果是新号码,则添加一个新的客户 ...

  5. grep经常使用使用方法

    grep简单介绍 正如linux的man文件里所描写叙述的那样: grep searches the named input FILEs (or standard input if no files ...

  6. Delphi和JAVA用UTF-8编码进行Socket通信例子

    最近的项目(Delphi开发),需要经常和java语言开发的系统进行数据交互(Socket通信方式),数据编码约定采用UTF-8编码. 令我无语的是:JAVA系统那边反映说,Delphi发的数据他们收 ...

  7. 保持长宽比 对背景图像进行修改android:scaleType="fitXY"

    关于android中ImageView的外观,即图片在其内显示出的样子,与布局文件中adjustViewBonds和scaleType属性的关系.我进行了一些探索.现跟大家共享,欢迎各位指教.分别将a ...

  8. 如何利用PhoneGap制作地图APP

    摘要:百度地图API是一套由javascript编写的地图程序接口,按说它应该运行在浏览器上.现在,只要利用PhoneGap,我们就能开发出移动平台上能使用的APP了! --------------- ...

  9. JAVA+FlexPaper+OpenOffice+SWFTools文档预览

    http://blog.csdn.net/core_star/article/details/10148047 1.软件环境: openoffice:启动openoffice服务:soffice.ex ...

  10. centos 服务器配置(一) 之端口占用

    1.查找被占用的端口 netstat -tln netstat -tln | grep 8060 netstat -tln 查看端口使用情况,而netstat -tln | grep 8060则是只查 ...