引起stat总是返回-1的原因

来源:互联网 发布:淘宝买便宜的东西的app 编辑:程序博客网 时间:2024/06/10 01:32

最近要写一个遍历目录,读取目录下全部文件(不包括"."和"..")属性的函数,函数的作用就像
使用命令"ls -l"得到的结果一样,但是需要考虑执行效率,哪怕提升一点点也好,因此不想使用
system("ls -l /dat >/file.txt")函数来调用系统命令得到一个文件再分析文件,因此写代
码如下:
int GetFileInfoStat(char *FileName,char *resultStr,struct stat *StatBuf){
  char date[16];
  char mode[11] = "----------";
  struct passwd *PassInfo;
  struct group *GroupInfo;
  resultStr[0]='/0';
  PassInfo=getpwuid(StatBuf->st_uid);//#include <sys/types.h>,<pwd.h>
  if(PassInfo!=NULL){
    GroupInfo=getgrgid(StatBuf->st_gid);//#include <sys/types.h>,<grp.h>
    if(GroupInfo!=NULL) {
      int b_mask = StatBuf->st_mode&S_IFMT;
      if(b_mask==S_IFDIR) {
        mode[0]='d';
      }
      else if(b_mask==S_IFREG){
        mode[0]='-';
      }
      else{
        return(FALSE);
      }
      mode[1]=(StatBuf->st_mode&S_IRUSR)?'r':'-';
      mode[2]=(StatBuf->st_mode&S_IWUSR)?'w':'-';
      mode[3]=(StatBuf->st_mode&S_IXUSR)?'x':'-';
      mode[4]=(StatBuf->st_mode&S_IRGRP)?'r':'-';
      mode[5]=(StatBuf->st_mode&S_IWGRP)?'w':'-';
      mode[6]=(StatBuf->st_mode&S_IXGRP)?'x':'-';
      mode[7]=(StatBuf->st_mode&S_IROTH)?'r':'-';
      mode[8]=(StatBuf->st_mode&S_IWOTH)?'w':'-';
      mode[9]=(StatBuf->st_mode&S_IXOTH)?'x':'-';
      strftime(date,13,"%b %d %H:%M",localtime(&(StatBuf->st_mtime)));
          
      sprintf(resultStr,"%s %3d %-4s %-4s %8d %12s %s/r/n",
              mode,StatBuf->st_nlink,PassInfo->pw_name,GroupInfo->gr_name,
              StatBuf->st_size,date,FileName);
      printf("%s",resultStr);
      return(TRUE);
    }
 }
 return(FALSE);
}
//对给定的文件路径进行一次扫描,得到此路径下文件(不包括文件".","..")的详细信息
//本函数非线程安全
int SendDirInfo(char *Dir){ 
  int i,namelen,status;
  DIR* dirp;
  struct dirent* direntp;
  struct stat StatBuf;
  char aFileInfo[250];
 
  dirp = opendir(Dir);//opendir:线程安全
  if(dirp!=NULL){
    for(;;){
      direntp=readdir(dirp);//Readdir:非线程安全
      if(direntp==NULL) break;
      namelen=strlen(direntp->d_name);
      if (!  (( (namelen==1)&&(direntp->d_name[0]=='.') )//非"."或".."
        ||( (namelen==2)&&(direntp->d_name[0]=='.')&&(direntp->d_name[1]=='.') ) )){
        status=stat(direntp->d_name,&StatBuf);
        printf("Status=%d,%s/n",status,direntp->d_name);//此处后来添加
        if(status==0){
          if (GetFileInfoStat(direntp->d_name,aFileInfo,&StatBuf)!=TRUE){
            printf("Error Get File Info of %s",direntp->d_name);
          }         
        }//end if(status==0); 
      }//end if d_name!=(. || ..)
    }//end for
    closedir(dirp);
    return(0);
  }//end if(dirp!=NULL)
  return(1);
}
可调试的时候,status值总是-1
在网上查了半天资料,也没有找到原因.
将status值与文件名打印出来,如下:
-1,file01.dat
-1,file02.dat
-1,file03.dat
...
这让人非常纳闷:函数stat只需要两个参数:一个文件名,一个结果存储区指针,这两处都没有问题
呀,可为什么总是返回-1呢.
后来灵机一动,看了一下文件名:只有相对路径名,试试绝对路径吧
于是将函数修改如下:

//对给定的文件路径进行一次扫描,得到此路径下文件的详细信息
int SendDirInfo(char *Dir){ 
  int i,namelen,status;
  DIR* dirp;
  struct dirent* direntp;
  struct stat StatBuf;
  char aFileInfo[250];
  char aFileAbsPath[250];//文件的绝对路径
 
  dirp = opendir(Dir);//opendir:线程安全
  if(dirp!=NULL){
    for(;;){
      direntp=readdir(dirp);//Readdir:非线程安全
      if(direntp==NULL) break;
      namelen=strlen(direntp->d_name);
      if (!  (( (namelen==1)&&(direntp->d_name[0]=='.') )////非"."或".."
           ||( (namelen==2)&&(direntp->d_name[0]=='.')&&(direntp->d_name[1]=='.') ) )){
        sprintf(aFileAbsPath,"%s/%s",Dir,direntp->d_name);    
        status=stat(aFileAbsPath,&StatBuf);
        printf("Status=%d,%s/n",status,aFileAbsPath);
        if(status==0){
          if (GetFileInfoStat(aFileAbsPath,aFileInfo,&StatBuf)!=TRUE){
            printf("Error Get File Info of %s",direntp->d_name);
          }         
        }//end if(status==0); 
      }//end if d_name!=(. || ..)
    }//end for
    closedir(dirp);
    return(0);
  }//end if(dirp!=NULL)
  return(1);
}
结果说明stat函数的确只能接收绝对路径名.

原创粉丝点击