分页: 1 / 1

ubuntu下stat数据结构的问题

发表于 : 2011-04-21 9:33
yts

代码: 全选

#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未声明?

百思不得其解,请指教。

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

发表于 : 2011-04-21 11:01
linjiework
仔细检查你的代码。部分代码写的不完整。比如

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

应该是

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

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

发表于 : 2011-04-21 12:37
yts
确实是的,看来还是太马虎,谢谢楼上