清单文件目录在Ubuntu [英] Listing files in Directory in Ubuntu

查看:187
本文介绍了清单文件目录在Ubuntu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想列出当前目录的父目录中的文件,但是当我试图从终端我得到分割错误执行这个程序。什么我做错了?这里是code:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&dirent.h GT;INT主(INT ARGC,CHAR *的argv [])
{
   结构的dirent * dirpent;
   DIR * dirp;   如果(的argc!= 2)
   {
       的printf(斜面继续与程序\\ n);
       返回0;
   }   dirp =执行opendir(的argv [1]);   如果(dirp)
   {
       而(dirpent = READDIR(dirp)!= NULL)
           的printf(%S \\ n,dirpent-> d_name);       closedir(dirp);
   }
   返回0;
}


解决方案

  dirpent = READDIR(dirp)!= NULL

 (dirpent = READDIR(dirp))!= NULL

您当前的前pression被解析为 dirpent =(READDIR(dirp)!= NULL),这将设置 dirpent 0或1。

I am trying to list files in the parent directory of the current directory, but when I try to execute this program from terminal I get Segmentation Error.. What am I doing wrong? Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

int main(int argc, char *argv[])
{
   struct dirent *dirpent;
   DIR *dirp;

   if(argc!=2)
   {
       printf("Cant continue with the program\n");
       return 0;
   }

   dirp= opendir(argv[1]);

   if(dirp)
   {
       while(dirpent=readdir(dirp) !=NULL)
           printf("%s\n",dirpent->d_name);

       closedir(dirp);
   }
   return 0;
}

解决方案

dirpent=readdir(dirp) !=NULL

should be

(dirpent = readdir(dirp)) != NULL

Your current expression is parsed as dirpent = (readdir(dirp) != NULL), which will set dirpent to either 0 or 1.

这篇关于清单文件目录在Ubuntu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆