簡體   English   中英

為什么mkdir()后跟ofstream :: operator <<失敗,但權限被拒絕?

[英]Why does mkdir() followed by ofstream::operator<< fail with permission denied?

以下假定簡單的程序因errno 13: Permission denied失敗errno 13: Permission denied 我沒有看到或了解文件/目錄權限。 誰能幫助您確定問題所在?

前言

>whoami
usera
>cd ~
>mkdir abc
>ls -ld abc
drwxrwxr-x 2 usera usera 4096 May  2 16:36 abc
>cd abc

代碼(在當前目錄,即“ abc”目錄中)

//main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <sys/stat.h>
#include <sys/types.h>

int main( int argc, char* argv[] )
{
  const std::string path = "./foo/";
  int result = 0;
  errno = 0;

  result = mkdir( path.c_str(), 0666 );
  std::cout << result << ": " << errno << ": " << strerror( errno ) << std::endl;
  std::string tmp = path + "fooFile";
  std::ofstream ofs( tmp.c_str(), std::ofstream::out );
  ofs << "hello, world!";
  std::cout << std::boolalpha << ofs.good() << std::endl;
  ofs.close();
  std::cout << result << ": " << errno << ": " << strerror( errno ) << std::endl;
  return 0;
}

執行

>ls -l
total 4
-rw-rw-r-- 1 usera usera 585 May  2 16:39 main.cpp

>g++ --version
g++ (GCC) 4.8.3 20140911 (Red Hat 4.8.3-7)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

>g++ -g main.cpp && ./a.out
0: 0: Success
false
0: 13: Permission denied
>
>ls -l
total 52
-rwxrwxr-x 1 usera usera 41844 May  2 16:44 a.out
drw-rw-r-- 2 usera usera  4096 May  2 16:44 foo
-rw-rw-r-- 1 usera usera   585 May  2 16:39 main.cpp

該錯誤告訴我一些 RE:文件/目錄權限錯誤,但是我看不到。 ls輸出中,我發現所有文件都歸我所有,因此我不清楚為什么會發生權限拒絕錯誤。 請告訴我這里出了什么問題。

需要執行權限才能訪問目錄的內容。 將您的權限參數更改為0755。

如果您沒有執行就已閱讀,則可以枚舉目錄中的元素,但無法訪問它們。 沒有執行的寫操作對目錄沒有意義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM