freecplus frame - Directory Operations

A source code description

freecplus is a C / C ++ open source framework under a Linux system, go to the C language source code Technology Network (www.freecplus.net) download.

This article describes the operation of the directory function and class freecplus frame.

Declaration file functions and classes are freecplus / _freecplus.h.

Definition file functions and classes are freecplus / _freecplus.cpp.

Sample program located freecplus / demo directory.

Compiled rules file is freecplus / demo / makefile.

Second, create a directory

In Linux, if you want to create a "/tmp/aaa/bbb/ccc/ddd/data.xml" file, you must first create the "/ tmp / aaa / bbb / ccc / ddd", as follows:

1) If the "/ tmp" directory does not exist, create the "/ tmp".

2) If the "/ tmp / aaa" directory does not exist, create the "/ tmp / aaa".

3) If the "/ tmp / aaa / bbb" directory does not exist, create the "/ tmp / aaa / bbb".

4) If the "/ tmp / aaa / bbb / ccc" directory does not exist, create the "/ tmp / aaa / bbb / ccc".

5) If the "/ tmp / aaa / bbb / ccc / ddd" directory does not exist, create the "/ tmp / aaa / bbb / ccc / ddd".

6) Create a "/tmp/aaa/bbb/ccc/ddd/data.xml" file.

While these operations are not much technical content, but also very annoying.

MKDIR function creates a directory step by step according to the file or directory name absolute path.

Function declaration:

bool MKDIR(const char *pathorfilename,bool bisfilename=true);

Parameter Description:

pathorfilename: filename or directory name absolute path.

bisfilename: description of the type of pathorfilename, true-pathorfilename is the filename or directory name, the default value is true.

Return value: true- created successfully, false- creation fails, if a failure is returned, a reason there are about three cases: 1) lack of authority; 2) pathorfilename argument is not a valid filename or directory name; 3) insufficient disk space.

Example (demo30.cpp)

/*
 *  程序名:demo30.cpp,此程序演示freecplus框架中采用MKDIR函数根据绝对路径的文件名或目录名逐级的创建目录。
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include "../_freecplus.h"

int main()
{
  MKDIR("/tmp/aaa/bbb/ccc/ddd",false);   // 创建"/tmp/aaa/bbb/ccc/ddd"目录。

  MKDIR("/tmp/111/222/333/444/data.xml",true);   // 创建"/tmp/111/222/333/444"目录。
}

Third, access to files in the directory

freecplus frame to get the file from a directory and its subdirectories to the list of function information package CDir class.

Class declaration:

// 获取某目录及其子目录中的文件列表信息。
class CDir
{
public:
  char m_DirName[301];        // 目录名,例如:/tmp/root。
  char m_FileName[301];       // 文件名,不包括目录名,例如:data.xml。
  char m_FullFileName[301];   // 文件全名,包括目录名,例如:/tmp/root/data.xml。
  int  m_FileSize;              // 文件的大小,单位:字节。
  char m_ModifyTime[21];      // 文件最后一次被修改的时间,即stat结构体的st_mtime成员。
  char m_CreateTime[21];      // 文件生成的时间,即stat结构体的st_ctime成员。
  char m_AccessTime[21];      // 文件最后一次被访问的时间,即stat结构体的st_atime成员。
  char m_DateFMT[21];         // 文件时间显示格式,由SetDateFMT方法设置。

  vector<string> m_vFileName; // 存放OpenDir方法获取到的文件名(文件全名,包括目录名)列表。
  int m_pos;                  // 已读取m_vFileName容器的位置,每调用一次ReadDir方法m_pos加1。

  CDir();  // 构造函数。

  void initdata(); // 初始化成员变量。

  // 设置文件时间的格式,支持"yyyy-mm-dd hh24:mi:ss"和"yyyymmddhh24miss"两种,缺省是前者。
  void SetDateFMT(const char *in_DateFMT);

  // 打开目录,获取目录中的文件列表信息,存放于m_vFileName容器中。
  // in_DirName,待打开的目录名,采用绝对路径,如/tmp/root。
  // in_MatchStr,待获取文件名的匹配规则,不匹配的文件被忽略。
  // in_MaxCount,获取文件的最大数量,缺省值为10000个。
  // bAndChild,是否打开各级子目录,缺省值为false-不打开子目录。
  // bSort,是否对获取到的文件列表(即m_vFileName容器中的内容)进行排序,缺省值为false-不排序。
  // 返回值:如果in_DirName参数指定的目录不存在,OpenDir方法会创建该目录,如果创建失败,返回false,还有,如果当前用户对in_DirName目录下的子目录没有读取权限也会返回false,其它正常情况下都会返回true。
  bool OpenDir(const char *in_DirName,const char *in_MatchStr,const unsigned int in_MaxCount=10000,const bool bAndChild=false,bool bSort=false);

  // 这是一个递归函数,用于OpenDir()的调用,在CDir类的外部不需要调用它。
  bool _OpenDir(const char *in_DirName,const char *in_MatchStr,const unsigned int in_MaxCount,const bool bAndChild);

  // 从m_vFileName容器中获取一条记录(文件名),同时得到该文件的大小、修改时间等信息。
  // 调用OpenDir方法时,m_vFileName容器被清空,m_pos归零,每调用一次ReadDir方法m_pos加1。
  // 当m_pos小于m_vFileName.size(),返回true,否则返回false。
  bool ReadDir();

  ~CDir();  // 析构函数。
};

Captions member variables and functions CDir class has been described in detail in the class declaration.

Let's demonstrate the use of CDir class through a scenario.

The following script generates test directories and files before execution.

mkdir /tmp/root
mkdir /tmp/root/aaa
mkdir /tmp/root/bbb
cd freecplus
cp freecplus.* /tmp/root/.
cp demo/demo1* /tmp/root/aaa/.
cp demo/demo2* /tmp/root/bbb/.
cp demo/makefile /tmp/root/bbb/.

After executing the above script, a list of directories and files in / tmp / root directory is as follows:

Here Insert Picture Description

Example (demo32.cpp)

/*
 *  程序名:demo32.cpp,此程序演示freecplus框架中采用CDir类获取某目录及其子目录中的文件列表信息。
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include "../_freecplus.h"

int main()
{
  CDir Dir;

  if (Dir.OpenDir("/tmp/root","*.h,*cpp",100,true,true)==false)
  {
    printf("Dir.OpenDir(/tmp/root) failed.\n"); return -1;
  }

  while(Dir.ReadDir()==true)
  {
    printf("filename=%s,mtime=%s,size=%d\n",Dir.m_FullFileName,Dir.m_ModifyTime,Dir.m_FileSize);
  }
}

running result

Here Insert Picture Description

Precautions:

1) ReadDir after each method call, the output value CDir class member variables to the other screen, operation effect was observed.

2) in_MatchStr parameters OpenDir method is very important, widely used in the actual development.

3) If the file name dot. "" Beginning, OpenDir method will not read these files, if you want to read these files, you can modify the source code _OpenDir method.

in_MaxCount parameter 4) OpendDir method of setting the number of files in each directory scan, it is recommended not to exceed 10,000, and if the value is too large, open the directory of time will be longer, but also consume more memory.

bSort parameter 5) OpenDir setting whether m_vFileName containers, the sort consumes resources and time, you can not sorted not sorted.

Fourth, the copyright notice

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.
Source: C Language Technology Network (www.freecplus.net)
Author: Ethics code Agriculture

If the article typos, or content errors, or other suggestions and comments, please correct me message, thank you very much! ! !

Published 159 original articles · won praise 458 · views 120 000 +

Guess you like

Origin blog.csdn.net/wucz122140729/article/details/105187358