分类: C/C++
2014-08-03 15:48:32
原型:
#include
DIR *opendir(const char *dirname);
struct dirent *readdir(DIR *dirp);
int closedir(DIR *dirp);
DIR是directory stream,opendir函数返回dir流类型并供读取函数readdir调用;
readdir返回dirent结构体:
struct dirent
{
#ifndef __USE_FILE_OFFSET64
__ino_t d_ino;
__off_t d_off;
#else
__ino64_t d_ino;
__off64_t d_off;
#endif
unsigned short int d_reclen;
unsigned char d_type;
char d_name[256]; /* We must not include limits.h! */
};
d_reclen表示记录长度,d_type表示文件类型(具体见后面),d_name表示文件名;
closedir返回0表示关闭成功,-1表示失败。
dirent结构体中的d_tpye的值可以为以下枚举成员:
enum
{
DT_UNKNOWN = 0, //未知类型
# define DT_UNKNOWN DT_UNKNOWN
DT_FIFO = 1, //管道
# define DT_FIFO DT_FIFO
DT_CHR = 2, //字符设备文件
# define DT_CHR DT_CHR
DT_DIR = 4, //目录
# define DT_DIR DT_DIR
DT_BLK = 6, //块设备文件
# define DT_BLK DT_BLK
DT_REG = 8, //普通文件
# define DT_REG DT_REG
DT_LNK = 10, //连接文件
# define DT_LNK DT_LNK
DT_SOCK = 12, //套接字类型
# define DT_SOCK DT_SOCK
DT_WHT = 14 //
# define DT_WHT DT_WHT
};
示例:
#include
string testPath;
DIR* pDir = NULL;
struct dirent* ent = NULL;
pDir = opendir(testPath.c_str());
if (NULL == pDir){
return false;
}
while (NULL != (ent=readdir(pDir)))
{
if(ent->d_type == 8) //普通文件
{
...
}
}
...
closedir(pDir);
pDir = NULL;
windows平台下:
#include
CreateDirectory (char *DirName, SECURITY_ATTRIBUTES Attribs);
linux平台下:
#include
mkdir (const char *path, mode_t mode);
windows下创建目录的一个例子(VC)
using namespace System;
using namespace System::IO;
int main()
{
// Specify the directories you want to manipulate.
String^ path = "c:\\MyDir";
String^ target = "c:\\TestDir";
try
{
// Determine whether the directory exists.
if ( !Directory::Exists( path ) )
{
// Create the directory it does not exist.
Directory::CreateDirectory( path );
}
if ( Directory::Exists( target ) )
{
// Delete the target to ensure it is not there.
Directory::Delete( target, true );
}
// Move the directory.
Directory::Move( path, target );
// Create a file in the directory.
File::CreateText( String::Concat( target, "" ) );
// Count the files in the target directory.
Console::WriteLine( "The number of files in {0} is {1}", target, Directory::GetFiles( target )->Length );
}
catch ( Exception^ e )
{
Console::WriteLine( "The process failed: {0}", e );
}
}
VC实现计算该目录以及子目录下文件大小(byte)
// The following example calculates the size of a directory
// and its subdirectories, if any, and displays the total size
// in bytes.
using namespace System;
using namespace System::IO;
long DirSize( DirectoryInfo^ d )
{
long Size = 0;
// Add file sizes.
array
System::Collections::IEnumerator^ myEnum = fis->GetEnumerator();
while ( myEnum->MoveNext() )
{
FileInfo^ fi = safe_cast
Size += (long)fi->Length;
}
array
while ( myEnum->MoveNext() )
{
DirectoryInfo^ di = safe_cast
Size += DirSize( di );
}
return Size;
}
int main()
{
array
if ( args->Length != 2 )
{
Console::WriteLine( "You must provide a directory argument at the command line." );
}
else
{
DirectoryInfo^ d = gcnew DirectoryInfo( args[ 1 ] );
Console::WriteLine( "The size of {0} and its subdirectories is {1} bytes.", d, DirSize( d ) );
}
}
linux下创建目录的例子:
#include
#include
int status;
...
status = mkdir("/home/cnd/mod1", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
还可以使用boost的跨平台方案:
#include "boost/filesystem.hpp" // includes all needed Boost.Filesystem declarations
#include
using boost::filesystem; // for ease of tutorial presentation;
// a namespace alias is preferred practice in real code
create_directory( "foobar" );