1

ディレクトリの内容を一覧表示する単純な C プログラムを作成しています。非表示のみをリストする方法を知っている人はいますか? 次のコードは、ディレクトリからすべてのファイルを取得し、完璧に動作しますが、必要なのは隠しファイルだけです。ありがとう。

4

4 に答える 4

6

GNU/Linux では、隠しファイルはドットで始まります。

#include <string.h>

int is_hidden(const char *name)
{
  return name[0] == '.' &&
         strcmp(name, ".") != 0 &&
         strcmp(name, "..") != 0);
}

ファイルが読み取り専用かどうかを確認するには、stat関数を使用することをお勧めします。

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int is_readonly(const char *name)
{
  struct stat buf;

  stat(name, &buf);

  return buf->st_mode & /* ... */;
}
于 2013-02-02T15:52:06.690 に答える
3
  • ファイルが「非表示」かどうかを判断するには、その名前が.
  • ファイルが読み取り専用かどうかを判断するには、stat(2)権限を確認します ( st_mode)

または、注意すれば、2 番目のポイントとして使用できaccess(2)ます。そのファイルが必要な場合は、返されたopen(2)ものを信頼しないでくださいaccess(2)。一般的にaccess(2)は避けるべきです。

于 2013-02-02T15:47:03.937 に答える
1

ファイルが読み取り専用かどうかを確認するには、sys/stat.h (参照: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/sys/stat.h.html )を使用できます。

st_mode で & (バイナリ AND) 操作を実行するだけです。

struct stat st; 

if (stat(fileName, &st) == 0)
    cout << " user write permission: " << (st.st_mode & 00200 ) ;

出力が 0 の場合、ユーザーには書き込み権限がありません (読み取り専用)。それ以外の場合、ファイルは読み取り専用ではありません。

その他の許可ビット:

       S_IRWXU     00700   mask for file owner permissions
       S_IRUSR     00400   owner has read permission
       S_IWUSR     00200   owner has write permission
       S_IXUSR     00100   owner has execute permission
       S_IRWXG     00070   mask for group permissions
       S_IRGRP     00040   group has read permission
       S_IWGRP     00020   group has write permission
       S_IXGRP     00010   group has execute permission
       S_IRWXO     00007   mask for permissions for others
                           (not in group)
       S_IROTH     00004   others have read permission
       S_IWOTH     00002   others have write permission
       S_IXOTH     00001   others have execute permission
于 2014-07-02T11:15:49.703 に答える
-2

以下のコード スニペットは、ファイルがウィンドウで非表示になっているかどうかを確認するのに役立ちます。

int is_hiddenfile(char* file_name)
{
    FILE * batch_file = NULL;
    FILE * output_file = NULL;
    int count = 0;
    batch_file = fopen ("D:\\bat.bat", "w");
    fputs ("dir /ah ", batch_file);
    fputs (file_name, batch_file);
    fputs (" 2>D:\\out.txt", batch_file);
    fclose (batch_file);
    ShellExecuteA (NULL, "open", "D:\\bat.bat", NULL, NULL, SW_SHOWNORMAL);
    output_file = fopen("D:\\out.txt", "r");
    while(feof(output_file) == 0)
    {
        fgetc(output_file);
        count++;
    }
    if (count <= 1)
        return 1;
    else
        return 0;
}

Windows でファイルが読み取り専用かどうかを確認するには、コマンド/arの代わりに使用する必要があります。/ahdir

int is_readonlyfile(char* file_name)
{
    ... //same as is_hiddenfile function
    fputs ("dir /ar ", batch_file);
    ... //same as is_hiddenfile function
}
于 2013-02-02T18:24:38.373 に答える