免费注册 查看新帖 |

Chinaunix

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

solaris 9.0下的获取目录编程问题请教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-05-11 17:08 |只看该作者 |倒序浏览
/*获取目录信息*/
/*file scandir.c*/
#include <stdio.h>;
#include <stdlib.h>;
#include <unistd.h>;
#include <errno.h>;
#include <sys/types.h>;
#include <dirent.h>;
#include <string.h>;

/*extern int scandir(const char *dirname, struct dirent ***namelist, \
   int (*select)(struct dirent *), int (*compar)(const void *, const void *));
*/
extern int scandir(const char *dirname, struct dirent *(*namelist[]),int (*select)(struct dirent *), int (*compar)(struct dirent **, struct dirent **));

extern int alphasort(const void *d1, const void *d2);

/*
* Select only those direntory entries that start with
* 'h'to demonstrate the selection ability :
*/

static int my_select(struct dirent *dp)
{
  if ( dp->;d_name[0] != 'h')
    return 0; /* Don't include this */
  return 1; /* else include this one*/
}

/*
* Sort entries in reverse order for demonstration
* purposes :
*/
static int my_compar(struct dirent **d1,struct dirent **d2)
{
  struct dirent *dir1 = *d1;
  struct dirent *dir2 = *d2;
  /*
   * Reverse the comparison by reversing
   * dir2 with dir1 in the strcmp(3) call:
   */
  return strcmp(dir2->;d_name,dir1->;d_name);
}

/*
* A good test is the direntory /etc :
*/
int main(int argc,char **argv)
{
  int x; /* Work index */
  int n; /* namelist[n] */
  struct dirent **namelist; /* List of names */
  
  if ( argc < 2 )
  {
    fputs("A pathname argument is required.\n, Try /etc for the direntory.\n",stderr);
    return 1;
  }
  
  /*
   * Scan the direntory given :
   */
  n = scandir(argv[1],&namelist,my_select,my_compar);
  
  /*
   * Report the direntory entries :
   */
  printf("%d entries for %s:\n",n,argv[1]);
  for ( x=0; x<n; ++x )
    printf("%3d: %s\n",x,namelist[x]->;d_name);
   
  if ( n >; 0 )
  {
    for ( x=0; x<n; ++x )
      free(namelist[x]); /* Release entry */
    free(namelist); /* Release the array */
  }
  return 0;
}

编译
$g++ scandir.c
后返回下面的错误

未定义                  文件中的
符号                       在文件中
scandir(char const*, dirent***, int (*)(dirent*), int (*)(dirent**, dirent**))/var/tmp//ccdpIoK3.o
ld: 致命的: 符号参照错误. 没有输出被写入a.out
collect2: ld returned 1 exit status

是什么错误呀?请指教。

论坛徽章:
0
2 [报告]
发表于 2003-05-11 17:09 |只看该作者

solaris 9.0下的获取目录编程问题请教

链接错误,是少了库文件吗

论坛徽章:
0
3 [报告]
发表于 2003-05-11 17:46 |只看该作者

solaris 9.0下的获取目录编程问题请教

未定义 文件中的
符号 在文件中


include对应头文件

看看man要include什么头文件

论坛徽章:
0
4 [报告]
发表于 2003-05-11 18:06 |只看该作者

solaris 9.0下的获取目录编程问题请教

偶查了scandir 这个函数man,需要include <sys/types.h>;和<sys/dir.h>;头文件,后面一个sys/dir.h找遍了都没有,
是不是这个的原因?

论坛徽章:
0
5 [报告]
发表于 2003-05-11 18:16 |只看该作者

solaris 9.0下的获取目录编程问题请教

自己写include这个文件
不管找得到没有
编译器说找不到这个文件再说

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
6 [报告]
发表于 2003-05-12 09:15 |只看该作者

solaris 9.0下的获取目录编程问题请教

偶查了scandir 这个函数man,需要include <sys/types.h>;和<sys/dir.h>;头文件,后面一个sys/dir.h找遍了都没有

这种情况需要验证,或许系统提供了编译开关。对于man出来的结果,到相应得目录下面找不到函数的情况不是很多的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP