Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
C でファイル ポインタが NULL を返した場合、ファイルが存在しないか、アクセス権が存在しないかを確認するにはどうすればよいですか? Linuxでコードを書いています。ファイルにはアクセス権がありませんが、ファイルは存在するため、ファイルが存在しない、またはファイルにアクセス権がないという別のステータスを返すにはどうすればよいですか。
errnoファイルを開こうとした後の値を確認します。
errno
if (NULL == (fp = fopen("myfile.txt", "r"))) { if (ENOENT != errno) { fprintf(stderr, "Failed to open file: %s\n", strerror(errno)); } else { fprintf(stderr, "file does not exist\n"); } }