简单C应用程序给出了分段错误

简单C应用程序给出了分段错误,c,segmentation-fault,C,Segmentation Fault,C从来不是我的一支强大的力量,但我决定试一试。下面是我的代码,但当运行时,它给出了一个分段错误(内核转储) 基本上,我想要的是检查文件夹是否为空(其中装载了mtp设备),如果为空,则运行mount命令,如果不是,则运行另一个命令 #include <sys/types.h> #include <dirent.h> #include <libgen.h> #include <libnotify/notify.h> #include <stdio

C从来不是我的一支强大的力量,但我决定试一试。下面是我的代码,但当运行时,它给出了一个分段错误(内核转储)

基本上,我想要的是检查文件夹是否为空(其中装载了mtp设备),如果为空,则运行mount命令,如果不是,则运行另一个命令

#include <sys/types.h>
#include <dirent.h>
#include <libgen.h>
#include <libnotify/notify.h>
#include <stdio.h>
#include <unistd.h>

void main ()
{
  int n = 0;
  struct dirent *d;
  const char *dir_path="~/Nexus";
  DIR *dir = opendir(dir_path);
      while ((d = readdir(dir)) != NULL) {
        if(++n > 2)
          break;
      }
      closedir(dir);
      if (n <= 2) //Directory Empty
    {
    notify_init ("Galaxy Nexus mounter");
    NotifyNotification * Mount = notify_notification_new ("Galaxy Nexus", "Mounted at ~/Nexus", "/home/tristan202/bin/test/android_on.png");
    system("jmtpfs ~/Nexus");
    notify_notification_show (Mount, NULL);
    }
      else
    {
    notify_init ("Galaxy Nexus mounter");
    NotifyNotification * uMount = notify_notification_new ("Galaxy Nexus", "Unmounted", "/home/tristan202/bin/test/android_off.png");
    system("fusermount -u ~/Nexus");
    notify_notification_show (uMount, NULL);
    }
    }
#包括
#包括
#包括
#包括
#包括
#包括
空干管()
{
int n=0;
结构方向*d;
const char*dir_path=“~/Nexus”;
DIR*DIR=opendir(DIR\u路径);
而((d=readdir(dir))!=NULL){
如果(++n>2)
打破
}
closedir(dir);
if(n2)
打破
}
closedir(dir);

如果(n您有几个问题:

  • 您不检查错误。如果
    opendir(2)
    无法打开目录并返回
    NULL
    ,则继续,将
    NULL
    传递到
    readdir(2)
    可能会导致它发生故障。这可能是因为
  • 文件系统不理解您在编写
    “~”
    时的意思,如在
    “~/Nexus”
    中。它试图打开一个名为
    “~/Nexus”的文件
    ~
    字符在文件系统中没有特殊意义——它对shell来说意味着什么。shell是执行的一个。为了获得所需的文件,您需要使用正确的完整绝对路径或正确的相对路径;您可以在运行时使用它来查找自己的主目录。请注意,这是可以的在调用
    system(3)
    函数时使用
    ~
    ,因为
    system
    调用shell
  • main()
    声明不正确。它必须返回
    int
    ,而不是
    void

  • 我肯定认为它会出现Seg故障,因为您忽略了检查当前目录和父目录。您需要在读取目录时继续,并查看“.”和“.”这就是为什么我将“n”设置为大于或小于2。这还不够好吗?我已经用您建议的更改编辑了主要帖子。现在它按预期工作,但我将继续工作,使它在其他机器上工作,即使用getenv,而不是我所做的。
    #include <sys/types.h>
    #include <dirent.h>
    #include <libgen.h>
    #include <libnotify/notify.h>
    #include <stdio.h>
    #include <unistd.h>
    
    int main ()
    {
      int n = 0;
      struct dirent *d;
      const char *dir_path="/home/tristan202/Nexus";
      DIR *dir = opendir(dir_path);
          while ((d = readdir(dir)) != NULL) {
            if(++n > 2)
              break;
          }
          closedir(dir);
          if (n <= 2) //Directory Empty
        {
        notify_init ("Galaxy Nexus mounter");
        NotifyNotification * Mount = notify_notification_new ("Galaxy Nexus", "Mounted at ~/Nexus", "/home/tristan202/bin/test/android_on.png");
        system("jmtpfs ~/Nexus");
        notify_notification_show (Mount, NULL);
        }
          else
        {
        notify_init ("Galaxy Nexus mounter");
        NotifyNotification * uMount = notify_notification_new ("Galaxy Nexus", "Unmounted", "/home/tristan202/bin/test/android_off.png");
        system("fusermount -u ~/Nexus");
        notify_notification_show (uMount, NULL);
        }
        }