linux下c语言编程 打印文件类型,总是出现段错误,查不出错误,求大神
intmain(intargc,char*argv[]){inti;structstatbuf;char*ptr;if(argc!=2)perror("file"),ex...
int main(int argc, char *argv[])
{
int i;
struct stat buf;
char *ptr;
if(argc != 2)
perror("file"),exit(-1);
for(i = 0; i < argc; i++)
{
printf("%s: ", argv[1]);
if(lstat(argv[1], &buf) < 0 )
{
perror("lstat()"),exit(-1);
continue;
}
if(S_ISREG(buf.st_mode))
ptr = "regular";
if(S_ISDIR(buf.st_mode))
ptr = "directory";
if(S_ISCHR(buf.st_mode))
ptr = "character special";
if(S_ISBLK(buf.st_mode))
ptr = "block ";
if(S_ISFIFO(buf.st_mode))
ptr = "fifo";
if(S_ISLNK(buf.st_mode))
ptr = "link";
printf("%s ", *ptr);
}
exit(0); 展开
{
int i;
struct stat buf;
char *ptr;
if(argc != 2)
perror("file"),exit(-1);
for(i = 0; i < argc; i++)
{
printf("%s: ", argv[1]);
if(lstat(argv[1], &buf) < 0 )
{
perror("lstat()"),exit(-1);
continue;
}
if(S_ISREG(buf.st_mode))
ptr = "regular";
if(S_ISDIR(buf.st_mode))
ptr = "directory";
if(S_ISCHR(buf.st_mode))
ptr = "character special";
if(S_ISBLK(buf.st_mode))
ptr = "block ";
if(S_ISFIFO(buf.st_mode))
ptr = "fifo";
if(S_ISLNK(buf.st_mode))
ptr = "link";
printf("%s ", *ptr);
}
exit(0); 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询