- 论坛徽章:
- 0
|
/*******************************************************************************
* header file
* int mkdir( const char *path, mode_t mode ); mode & (~umask), return 0/-1
* int rmdir( char *path ); rm an empty dir(nlink=0), return 0/-1
* header file
* char *getcwd( char *buf, size_t size ); get current absolute path
* char *getwd( char *pathname ); copy current path to pathname
* DIR *opendir( const char *dirname ); return DIR stream pointer or NULL
* struct dirent *readdir( DIR *dirp ); return dirent pointer or NULL
* int closedir( DIR *dirp ); close dir stream and release DIR struct
*
* void seekdir( DIR *dirp, long int loc ); can't be assigned,loc=telldir()
* void rewinddir( DIR *dirp );
* long int telldir( DIR *dirp );
*
*******************************************************************************/
#include stdio.h>
#include unistd.h>
#include dirent.h>
int main( int ac, char *av[] )
{
char buf[255], *dir = "dir";
long int loc = 0;
DIR *pdir = NULL;
struct dirent *pent = NULL;
fprintf( stderr, "pwd = [%s]\n", getcwd(buf, sizeof(buf)) );
fprintf( stderr, "create dir [%s], respcode = [%d]\n",
dir, mkdir(dir, 0777) );
chdir("./dir");
fprintf( stderr, "pwd = [%s]\n", getcwd(buf, sizeof(buf)) );
chdir("../");
if ( (pdir = opendir("./")) == NULL )
{
fprintf( stderr, "open dir failed.\n" );
return 1;
}
loc = telldir( pdir );
fprintf( stderr, "current location: [%ld]\n", loc );
while (1)
{
pent = readdir( pdir );
if ( pent == NULL )
break;
fprintf( stderr, "%5ld %s\n", pent->d_ino, pent->d_name );
}
seekdir( pdir, loc );
rewinddir( pdir ); //seekdir( pdir, 0 );
closedir( pdir );
rmdir(dir);
return 0;
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/107272/showart_2114075.html |
|