- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我在使用 realpath
函数时遇到了一个奇怪的问题。该函数在给定一个作为程序参数接收的字符串时工作,但在给定我在源代码中定义的字符串时失败。这是一个简单的程序:
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
int main(int argc, const char* argv[])
{
char* fullpath = (char*)malloc(PATH_MAX);
if(realpath(argv[1], fullpath) == NULL)
{
printf("Failed\n");
}
else
{
printf("%s\n", fullpath);
}
}
当我使用参数 ~/Desktop/file
运行它时(file
存在并且是一个常规文件)我得到了预期的输出
/home/<username>/Desktop/file
这是程序的另一个版本:
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
int main(int argc, const char* argv[])
{
const char* path = "~/Desktop/file";
char* fullpath = (char*)malloc(PATH_MAX);
if(realpath(path, fullpath) == NULL)
{
printf("Failed\n");
}
else
{
printf("%s\n", fullpath);
}
}
当我运行这个程序时,我得到了输出
Failed
为什么第二个失败了?
最佳答案
const char* path = "~/Desktop/file";
波浪字符(即:~
)未被扩展(即:替换为您的主目录的路径) 在你的程序中。
当您像在第一个程序中一样在命令行中将它作为参数提供时,它会由 shell 扩展。
关于C realpath 函数不适用于源文件中定义的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44311220/
出于性能原因,我们应该只使用 realpath()而不是 realpath() + file_exists()在检查文件或目录是否存在时 ?? 案例 A:: if(realpath(__DIR__."
做这样的事情是不是一种感觉 $dir = realpath(dirname(__FILE__)); // or for php 5.3+: $dir = realpath(__DIR__) 或者
调用 Unix realpath() 函数的传统方法是 realpath(pathname, buf) 其中 buf 是用户提供的PATH_MAX 字节空间的缓冲区。这是有问题的,因为 PATH_MA
这个问题在这里已经有了答案: Unexpected behavior in printing a char array in C (1 个回答) 关闭 8 年前。 这是我的功能,它在给定目录中查找常
PHP 的 realpath() 在 manual 中有详细描述,我只是想了解它在哪些场景下有用。因为看起来我的代码可以很好地处理 ../../path 这样的路径,所以我不确定 realpath()
我使用 MediaWiki,但出现错误。我在文件中找到了它。有一行代码: $IP = realpath( '.' ); 但是 $IP 返回一个空字符串,所以系统中文件的路径不起作用。 (例如,如果 $
我想检查一个路径是否是另一个路径的子路径,所以我调用 realpath(file_path, NULL) 并将结果与 strncmp 进行比较。 我写这个主题的原因是:realpath 设置err
我在使用 realpath 函数时遇到了一个奇怪的问题。该函数在给定一个作为程序参数接收的字符串时工作,但在给定我在源代码中定义的字符串时失败。这是一个简单的程序: #include #includ
我正在开发一个程序,它使用 realpath() 来获取文件的绝对路径。不幸的是,这个函数需要一个字符串缓冲区,这个缓冲区应该很大,以至于它足够大,当这个应用程序必须跨多个平台运行时,这是不安全的。是
给定 现代 Linux/UNIX/OSX(带 realpath) bash 4+(甚至在 OSX 上) 是 "$PWD" == "$(realpath .)" 总是正确的? 最佳答案 很容易测试情况并
我的 Windows 服务器上有一个符号链接(symbolic link),它是这样制作的: F:\>mkdir link-target F:\>mklink /D link f:\link-targ
当我编译以下代码时: #define _POSIX_C_SOURCE 200112L #define _ISOC99_SOURCE #define __EXTENSIONS__ #include #
我在使用 File.realpath() 时遇到问题,留下的字符串似乎没有被垃圾收集。在我看来,这像是内存泄漏,但我无法想象这样的事情对于核心库方法来说真的是真的。 考虑以下代码: def strin
我已经养成了使用以下命令在我的编辑器中快速打开相关文件进行代码审查的习惯。 git diff --name-only master... | xargs realpath | someEditor 但
我的一个扩展要求我将 realpath_cache_size 设置为至少 512k。那么我是否必须将它设置在 .htaccess 中,例如: realpath_cache_size = 512k 以及
我已经读过有关 realpath() 的内容,但是是否有一个函数可以传递基本目录和文件名,可以在不解析符号链接(symbolic link)或检查文件是否实际存在的情况下给出以下结果?或者我是否必须使
我已经阅读过有关 realpath() 的内容,但是是否有一个函数可以传递一个基本目录和一个文件名,从而在不解析符号链接(symbolic link)或检查文件是否实际存在的情况下给出以下结果?还是我
我想获取当前执行脚本的路径,我用过 os.path.realpath(__file__),但是,它会返回类似 D:\My Stuff\Python\my_script.py 的字符串,而没有适当的反斜
显然,realpath 有很多问题。在 PHP 5.3.1 中,它会导致随机崩溃。在 5.3.0 及更低版本中,realpath 随机失败并返回 false(当然是针对相同的字符串),而且它总是在 r
我有以下代码: os.chdir(os.path.dirname(os.path.realpath(__file__)) + "/../test") path.append(os.getcwd())
我是一名优秀的程序员,十分优秀!