- 论坛徽章:
- 0
|
还是关于C编程实现复制一个目录的问题
简单回答一下吧。
directory: closedir, opendir, readdir, rewinddir, seekdir, telldir -- directory operations
closedir- closes the named directory stream and frees the DIR structure
opendir- opens a directory
readdir- returns a pointer to the next active directory entry
rewinddir- resets the named directory stream to the beginning of the directory
seekdir- sets the position of the next readdir operation
telldir- returns current location associated with named directory stream
Syntax
cc . . . -lc
#include <dirent.h>;
int closedir(DIR *dirp);
DIR *opendir(const char *dirname);
struct dirent *readdir(DIR *dirp);
void rewinddir(DIR *dirp);
void seekdir(DIR *dirp, long int loc);
long int telldir(DIR *dirp);
Example
Sample code which searches a directory for entry name:
dirp = opendir( "." );
while ( (dp = readdir( dirp )) != NULL )
if ( strcmp( dp->;d_name, name ) == 0 )
{
closedir( dirp );
return FOUND;
}
closedir( dirp );
return NOT_FOUND;
文件名字放在d_name中。
但只有这些还是不够的,因为目录中的文件有很多种,但不是所有的都可以被复制。比如设备文件、目录、管道、名字文件都不能被复制。所以在读取之前还要用stat系统调用确定所对应文件的类型。只有规则文件和符号链接是可以被复制的。如果是符号链接还要判断是复制链接本身还是复制被链接的文件,如果被链接的文件也是一个符号链接呢? |
|