免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1004 | 回复: 0
打印 上一主题 下一主题

文件目录相关编程 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-08 14:49 |只看该作者 |倒序浏览

               
               
               
                /*******************************************************************************
* 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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP