使用stat函数遇到的有关问题,请诸位高人指点

使用stat函数遇到的问题,请诸位高人指点
在Linux系统中, 源代码如下, 功能是想查找record文件夹下的文件名, 以及文件的大小. 但是函数运行结束后,程序却自动退出了, 而如果把stat部分的代码屏蔽掉, 则程序就正常接着运行下去. 请各位高人讲解其中的原因.
[code=C/C++][/code]
int FileList()
{
int i;
int a, b;
int result;
char temp[50];
long temp_size;
  char list_name[40][50];
  long list_size[40];
DIR *dir_info;
struct dirent *dir_file;
struct stat *buf; 

i = 0;

dir_info = opendir("./record/");
if( dir_info != NULL )//打开目录成功
{
if( (dir_file = readdir(dir_info)) == NULL)  
{
show_info( "finding records error!" );
return i;
}  
while( (dir_file = readdir(dir_info)) != NULL )
{
 
  if( (strcmp(dir_file->d_name, "..") == 0) || (strcmp(dir_file->d_name, ".") == 0) )//忽略这两个特殊项
continue;

sprintf( list_name[i], "%s", dir_file->d_name );

sprintf( temp, "./record/%s", dir_file->d_name );
result = stat( temp, buf );
if( result != 0 )
{
show_info("Records initialization error!");
closedir(dir_info);
return i;
}
else
list_size[i] = buf->st_size; 

i++;
} // end of while
  
  closedir(dir_info); //使用完毕,关闭目录指针。
  }
}


struct stat *buf; 

buf是指针,内存呢? 没学好C就学Linux了?


Sorry,是我没讲明白。

探讨

引用:
C/C++ code
struct stat *buf; ———> struct stat buf;//搞成局部变量就可以啦。

result = stat( temp, &buf);

list_size[i] = buf.st_size; 

是我又写错了,不好意思,请帮我再解答下上面的吧,就是拆分成两个……

使用stat函数遇到的有关问题,请诸位高人指点

相关文章:

你感兴趣的文章:

标签云: