如何检查文件名和变量名是否相同

时间:2019-06-25 17:45:39

标签: c++11

实际上,问题是,系统升级时,应在根目录中写入“ DISABLE_BACKUP”文件。当它出现时,我必须检查文件是否已经在根目录中。

    if ((dir = opendir ("/"))!=NULL)
    {
        while ((ent = readdir(dir)) != NULL)
        {
            printf ("%s\n", ent->d_name);

        //Here i have to compare the filename (DISABLE_BACKUP) with the string "DISABLE_BACKUP" and has to raise log entry.
        }
    closedir(dir);
    }

1 个答案:

答案 0 :(得分:2)

  1. 比较字符串的C函数为strcmp()

    if (strcmp(ent->d_name, "DISABLE_BACKUP")==0) {
       // Found it!
    
  2. 也许更好的方法来查看文件“ DISABLE_BACKUP”是否存在为access()

    #include <unistd.h>
    ...
    if (access(fname, F_OK) != -1) {
       // file exists
    } else {
       // file doesn't exist
    }