Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 为什么赢了';t POSIX函数open()接受my path*变量_Linux_Posix - Fatal编程技术网

Linux 为什么赢了';t POSIX函数open()接受my path*变量

Linux 为什么赢了';t POSIX函数open()接受my path*变量,linux,posix,Linux,Posix,我从另一个用户那里借用了这段代码,试图理解posix函数,但当我要求它打开我在上面指定的文件名时,它会给我这个错误 int fd=-1; char *filename = "/home/user/POSIXfunctions/testfiles/testfile.txt"; //Arbitary size for the buffer?? How to optimise. //Dynamic allocation is a choice but what is the

我从另一个用户那里借用了这段代码,试图理解posix函数,但当我要求它打开我在上面指定的文件名时,它会给我这个错误

   int fd=-1;
   char *filename = "/home/user/POSIXfunctions/testfiles/testfile.txt";
   //Arbitary size for the buffer?? How to optimise.
   //Dynamic allocation is a choice but what is the
   //right way to relate the file size to bufffer size.

   fd=open(filename , O_WRONLY , mode);
   if(-1 == fd)
   {
      perror("Open Failed");
      return 1;
   }
为什么会这样?路径是正确的,没有拼写错误或任何东西。有人知道问题出在哪里吗?我希望能够给它的规格从根所有的方式


编辑:删除了一些不必要的变量

我复制了你的代码,并在我的Linux机器上运行了它,它正常工作了。所以我猜这个文件并不存在。 再次检查此文件是否存在(例如,您说您从路径中忽略了实际用户)
关于你关于库的问题-它与库无关。

我能想到你的程序不起作用的以下原因:

1) 文件不存在。正如上面所建议的,用YounC程序中给出的绝对路径运行ls-al应该会告诉您该文件是否存在


2) 您没有写入该文件的权限。运行chmod 666/path/to/file并尝试运行您的程序。检查权限是否存在问题的另一种方法是使用sudo运行程序。

检查您是否具有对文件的写入权限。在ls-ll:-rw-rw-r--1 user user 10 Sep 16 10:25 testfile.txt之后,所以,是的,我想我会。@user2824512您应该运行
ls-l/home/user/POSIXfunctions/testfiles/testfile.txt
您为什么选择该模式?我认为只有在使用O_create flag的情况下,open才是用S_I*modse定义的。有几个open(),大多数都是用一堆模式定义的,所以因为我不想截断或创建文件,所以我只留下O_wr。我试试不穿。至于运行ls-l path,我做到了,并且得到了
-rw-rw-r--1 user user user 10 Sep 16 10:25/home/user/POSIXfunctions/testfiles/testfile.txt
首先,感谢您迄今为止的帮助。不,不,我的意思是,为了这个问题,我把用户名改成了“user”,我确实是用同样的格式复制的,只是为了这个问题,我把用户名改了一点。你认为它能有任何东西与任何东西的过时版本一起使用吗?我在ubuntu 14.04上运行这个,如果有任何信息的话。它与库或版本无关。该文件不存在。从C文件复制确切的文件名并在其上运行ls。也许你会得到“没有这样的文件或目录”。
Open Failed: No such file or directory