ubuntu下stat数据结构的问题

软件和网站开发以及相关技术探讨
回复
yts
帖子: 9
注册时间: 2011-04-13 14:50

ubuntu下stat数据结构的问题

#1

帖子 yts » 2011-04-21 9:33

代码: 全选

#include "apue.h"
#include <sys/stat.h>

int main(int argc, char *argv[])
{
     int  i;
	 struct stat buf;
	 char *ptr;

	 for (i=1; i<argc; i++)
	 {
	     printf("%s: ", argv[i]);
		 if (stat(argv[i], &buf) < 0)
		 {		 
				 printf("lstat errno\n");
		                 continue;
		 }
	 
	 if (S_ISREG(buf.st_mode))
			 ptr="regular";
	 else if (S_ISDIR(buf.st_mode))
			 ptr="directory";
	 else if (S_ISCHR(buf.st_mode))
			 ptr="character special";
	 else if (S_ISBLK(buf.st_mode))
			 ptr="block special";
	 else if (S_ISFIFO(st_mode))
			 ptr="fifo";
	 else if (S_ISLNK(st_mode))
			 ptr="symbolic link";
	 else if (S_ISSOCK(st_mode))
			 ptr="socket";
	 else
			 ptr="unkown mode";
	 printf("%s\n", ptr);
     }
	 return 0;
}


APUE上的一段代码,编译时报错:

4-1.c: In function ‘main’:
4-1.c:27: error: ‘st_mode’ undeclared (first use in this function)
4-1.c:27: error: (Each undeclared identifier is reported only once
4-1.c:27: error: for each function it appears in.)

为什么说st_mode未声明?

百思不得其解,请指教。
头像
linjiework
帖子: 240
注册时间: 2009-07-07 19:52

Re: ubuntu下stat数据结构的问题

#2

帖子 linjiework » 2011-04-21 11:01

仔细检查你的代码。部分代码写的不完整。比如

...
else if (S_ISFIFO(st_mode))
...

应该是

...
else if (S_ISFIFO(buf.st_mode))
...
阿呆 : 天下第一呆!
yts
帖子: 9
注册时间: 2011-04-13 14:50

Re: ubuntu下stat数据结构的问题

#3

帖子 yts » 2011-04-21 12:37

确实是的,看来还是太马虎,谢谢楼上
回复