/**********
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. TMsgThread, TCommThread -- 在delphi线程中实现消息循环

    http://delphi.cjcsoft.net//viewthread.php?tid=635 在delphi线程中实现消息循环 在delphi线程中实现消息循环 Delphi的TThread类使 ...

  2. ghostDoct 使用 (转 http://www.cnblogs.com/RockyMyx/archive/2010/04/20/Project-Route-Using-GhostDoc.html)

    一.简介 GhostDoc是Visual Studio的一个免费插件,可以为开发人员自动生成XML格式的注释文档. 二.下载 需要的朋友可以去这里下载,填个Email地址就可以下了:GhostDoc下 ...

  3. java 正则表达式学习

    一. Java正则表达式 在程序开发中,难免会遇到需要匹配.查找.替换.判断字符串的情况发生,而这些情况有时又比较复杂. 因此,学习及使用正则表达式,便成了解决这一矛盾的主要手段. 正则表达式是一种可 ...

  4. UVALive 4225 Prime Bases 贪心

    Prime Bases 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&a ...

  5. Codeforces Round #313 (Div. 1) B. Equivalent Strings DFS暴力

    B. Equivalent Strings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/559 ...

  6. 查看浏览记录(thinkphp)

    if ((MODULE_NAME == 'Goods' || MODULE_NAME == 'News') && ACTION_NAME == 'info' && $t ...

  7. iOS开发——开发必备OC篇&彩票实战之精华讲解

    彩票实战之精华讲解 在这段时间自己研究并学习关于彩票项目开发的时候遇到各种坑,各种技术点以前或许之前用过但是却用起来不是那么熟悉,所以没遇到一个重点的地方我就会记录一下,希望不会再有下次. 本文主要讲 ...

  8. 删除sqlserver2008登录记录

    SQL Server Management Studio登陆窗口 清空这些多余的登陆名 删除SqlStudio.bin文件 WinXP: C:\Documents and Settings\用户文件夹 ...

  9. 解决Github使用Fastly CDN而导致不能加载网页的方法 转自 沙丘:http://www.enkoo.net/fastly-cdn-in-gifhub.html

    Github现在基本属于“安全”网站,但Github使用fastly.net的CDN服务后,其网站在国内经常不能正常加载网页.github.global.ssl.fastly.net的亚洲IP一般为1 ...

  10. Java_Hbase优化

    1.datanode的最大文件数 vi $HADOOP_HOME/conf/hdfs-site.xml <property> <name>dfs.datanode.max.xc ...