Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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 open(const char*path,int of lag)不';t接受类似“的路径”~/“我的.log”;_Linux_Io_Posix - Fatal编程技术网

Linux open(const char*path,int of lag)不';t接受类似“的路径”~/“我的.log”;

Linux open(const char*path,int of lag)不';t接受类似“的路径”~/“我的.log”;,linux,io,posix,Linux,Io,Posix,我试着用 open("~/my.log", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) 创建文件,但返回错误 "No such file or directory". 是因为open()不翻译“~”吗?你是对的。是shell翻译~,而不是open。这同样适用于通配符,如*和?。事实上,任何时候你都可以键入除磁盘上的字节以外的任何内容作为文件名,正是你的shell为你翻译成了一个

我试着用

open("~/my.log",  O_WRONLY | O_CREAT | O_TRUNC,
        S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) 
创建文件,但返回错误

"No such file or directory". 

是因为open()不翻译“~”吗?

你是对的。是shell翻译
~
,而不是
open
。这同样适用于通配符,如
*
。事实上,任何时候你都可以键入除磁盘上的字节以外的任何内容作为文件名,正是你的shell为你翻译成了一个实际的文件名。

你是正确的。是shell翻译
~
,而不是
open
。这同样适用于通配符,如
*
。事实上,任何时候你都可以键入除磁盘上的字节以外的任何内容作为文件名,正是你的shell为你翻译成了一个实际的文件名。

你是正确的。是shell翻译
~
,而不是
open
。这同样适用于通配符,如
*
。事实上,任何时候你都可以键入除磁盘上的字节以外的任何内容作为文件名,正是你的shell为你翻译成了一个实际的文件名。

你是正确的。是shell翻译
~
,而不是
open
。这同样适用于通配符,如
*
。事实上,任何时候你都可以键入除磁盘上的字节以外的任何内容作为文件名,正是你的shell为你翻译成了一个实际的文件名。

是的,没错

波浪线是shell路径名扩展的一部分。打开库调用不使用shell。

是的,完全正确

波浪线是shell路径名扩展的一部分。打开库调用不使用shell。

是的,完全正确

波浪线是shell路径名扩展的一部分。打开库调用不使用shell。

是的,完全正确

波浪线是shell路径名扩展的一部分。open library调用不使用shell。

您可以(如注释所示)构造(使用或使用,即
getenv(“HOME”)
)所需路径,或者使用函数将
“~/my.log”
扩展到所需路径。另请参见
GLOB_TILDE

因此,您可以编写以下代码:

char logpathbuf[256];
snprintf (logpathbuf, sizeof(logpathbuf), "%s/my.log", getenv("HOME"));
int fd = open(logpathbuf,  O_WRONLY | O_CREAT | O_TRUNC,
              S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd <0) { perror("open logfile"); exit(EXIT_FAILURE); }
charlogpathbuf[256];
snprintf(logpathbuf,sizeof(logpathbuf),“%s/my.log”,getenv(“HOME”);
int fd=打开(logpathbuf,O|u WRONLY | O|u CREAT | O|u TRUNC,
S|IRUSR | S|IWUSR | S|IRGRP | S|IROTH);
如果(fd您可以(如注释所示)构造(使用或使用,即
getenv(“HOME”)
)所需的路径,或者使用函数将
“~/my.log”
扩展到所需的路径。另请参见使用
GLOB\u TILDE

因此,您可以编写以下代码:

char logpathbuf[256];
snprintf (logpathbuf, sizeof(logpathbuf), "%s/my.log", getenv("HOME"));
int fd = open(logpathbuf,  O_WRONLY | O_CREAT | O_TRUNC,
              S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd <0) { perror("open logfile"); exit(EXIT_FAILURE); }
charlogpathbuf[256];
snprintf(logpathbuf,sizeof(logpathbuf),“%s/my.log”,getenv(“HOME”);
int fd=打开(logpathbuf,O|u WRONLY | O|u CREAT | O|u TRUNC,
S|IRUSR | S|IWUSR | S|IRGRP | S|IROTH);
如果(fd您可以(如注释所示)构造(使用或使用,即
getenv(“HOME”)
)所需的路径,或者使用函数将
“~/my.log”
扩展到所需的路径。另请参见使用
GLOB\u TILDE

因此,您可以编写以下代码:

char logpathbuf[256];
snprintf (logpathbuf, sizeof(logpathbuf), "%s/my.log", getenv("HOME"));
int fd = open(logpathbuf,  O_WRONLY | O_CREAT | O_TRUNC,
              S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd <0) { perror("open logfile"); exit(EXIT_FAILURE); }
charlogpathbuf[256];
snprintf(logpathbuf,sizeof(logpathbuf),“%s/my.log”,getenv(“HOME”);
int fd=打开(logpathbuf,O|u WRONLY | O|u CREAT | O|u TRUNC,
S|IRUSR | S|IWUSR | S|IRGRP | S|IROTH);
如果(fd您可以(如注释所示)构造(使用或使用,即
getenv(“HOME”)
)所需的路径,或者使用函数将
“~/my.log”
扩展到所需的路径。另请参见使用
GLOB\u TILDE

因此,您可以编写以下代码:

char logpathbuf[256];
snprintf (logpathbuf, sizeof(logpathbuf), "%s/my.log", getenv("HOME"));
int fd = open(logpathbuf,  O_WRONLY | O_CREAT | O_TRUNC,
              S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd <0) { perror("open logfile"); exit(EXIT_FAILURE); }
charlogpathbuf[256];
snprintf(logpathbuf,sizeof(logpathbuf),“%s/my.log”,getenv(“HOME”);
int fd=打开(logpathbuf,O|u WRONLY | O|u CREAT | O|u TRUNC,
S|IRUSR | S|IWUSR | S|IRGRP | S|IROTH);

如果(fd)我大致记得fopen()可以处理这样的路径名,对吗?@SetTimer:No,fopen()也不使用shell。我大致记得fopen()可以处理这样的路径名,对吗?@SetTimer:No,fopen()也不使用shell。我大致记得fopen()可以处理这样的路径名,对吗?@SetTimer:No,fopen()也不使用shell。我大致记得fopen()可以处理这样的路径名,对吗?@SetTimer:不,fopen()也不使用shell。在shell中,
~/my.log
“$HOME/my.log”相同
。您可以在C中获取
HOME
环境变量并使用它来构造所需的路径。请参阅。在shell中,
~/my.log
“$HOME/my.log”
相同。您可以在C中获取
HOME
环境变量并使用它来构造所需的路径。请参阅。在shell中,
~/my.log
相同“$HOME/my.log”
。您可以在C中获取环境变量
HOME
并使用它来构造所需的路径。请参阅。在shell中,
~/my.log
“$HOME/my.log”
相同。您可以在C中获取环境变量
HOME
并使用它来构造所需的路径。请参阅。