首页 > 编程知识 正文

a love that will never grow old,you will leave the world behind

时间:2023-05-04 22:30:55 阅读:264927 作者:7

我对realpath函数有一个奇怪的问题.当函数被赋予一个作为程序参数接收的字符串时,该函数会起作用,但是当给出我在源代码中定义的字符串时,该函数会失败.这是一个简单的程序:

#include

#include

#include

int main(int argc, const char* argv[])

{

char* fullpath = (char*)malloc(PATH_MAX);

if(realpath(argv[1], fullpath) == NULL)

{

printf("Failedn");

}

else

{

printf("%sn", fullpath);

}

}

当我使用参数〜/ Desktop / file(文件存在且是常规文件)运行时,我得到了预期的输出

/home//Desktop/file

这是该程序的另一个版本:

#include

#include

#include

int main(int argc, const char* argv[])

{

const char* path = "~/Desktop/file";

char* fullpath = (char*)malloc(PATH_MAX);

if(realpath(path, fullpath) == NULL)

{

printf("Failedn");

}

else

{

printf("%sn", fullpath);

}

}

当我运行这个程序时,我得到输出

Failed

为什么第二个失败?

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。