兼容Mac fopen函数的问题 [英] Problems with fopen function on Mac

查看:251
本文介绍了兼容Mac fopen函数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在在Mac上一个简单的应用程序(OSX小牛,铿锵编译)。
问题是,当我尝试使用相对路径打开一个文件,这是行不通的。它的工作原理,如果我使用绝对路径。

I'm making a simple application on Mac (OSX Mavericks, compiling with clang). The problem is that when I try to open a file using the relative path, it doesn't work. It works if I use the absolute path.

例如,如果我尝试:计划生育=的fopen(file.txt的,R);

它返回 NULL

它只是如果我使用的作品:计划生育=的fopen(用户/用户名/ Project_folder / file.txt的,R);
(项目文件夹就是我在编译的文件夹,它有我的file.txt的)

It only works if I use: fp = fopen(" User/UserName/Project_folder/file.txt", "r"); (project folder is the folder that I'm compiling in, and it has my file.txt)

该文件是完全相同的目录。

The file is in the exactly same directory.

我该如何解决呢?

另外,同一code一直运作良好,前一段时间,现在这个和其他code,它使用了的fopen 显示了同样的问题。

Also the same code has worked well some time ago, and now this and other code that uses the fopen show the same problem.

推荐答案

当您运行的fopen 不完整的路径,它使用位置运行位置的当前工作目录从程序。所以,如果你的工作目录的你来自哪里的执行程序是〜/ my_folder /(通过键入 PWD 壳获得) file.txt的恰好是在〜/ my_folder /斌/ 那么,即使可执行文件是在〜/ my_folder /斌/ 它不会找到 file.txt的。因为它会寻找〜/ my_folder / file.txt的

When you run fopen without full path, it uses the current working directory of the location where you ran the program from. So if your working directory (obtained by typing pwd in shell) where you execute your program from is ~/my_folder/ and file.txt happens to be in ~/my_folder/bin/ then even if your executable is inside ~/my_folder/bin/ it won't find file.txt. Because it will be looking for ~/my_folder/file.txt.

所以,如果你是从一个不同的目录比file.txt的运行您的程序,那么你要么必须提供绝对路径,或者提供相对于你是从运行程序的目录的路径。

So if you are running your program from a different directory than that of file.txt then you either have to provide absolute path or provide a path relative to the directory that you are running your program from.

有关工作目录信息请参见 http://www.linfo.org/current_directory.html

For info on working directory see http://www.linfo.org/current_directory.html

更新

一个解决办法,如果你有机会获得的字符串数组参数(的argv [] ),可以使用方法。 ARGV的第一个元素将包含(从工作目录),这是用来执行程序的路径。下面这个简单的程序简单打印这样的说法。

One workaround that can be used if you have access to the string array argument (argv[]) of main method. The first element of argv will contain the path (from working directory) that was used to execute your program. Following simple program simple prints that argument.

int main(int argc, char *argv[])
{
  printf("Path relative to the working directory is: %s\n", argv[0]);
  return 0;
}

这将打印包括可执行文件名的相对路径。你可以用 file.txt的替换可执行文件的名称,如果该文件是在同一位置为可执行文件。这样,它会在两种情况下加载:当通过双击导航到可执行文件的位置运行命令行运行时,也

This will print the relative path including the executable name. You can replace executable name with file.txt if that file is in the same location as executable. This way it will load in both scenarios: when run by double-clicking and also when run from command line by navigating to the location of the executable.

您可能还需要考虑到可能的 ./ 年初的argv [0] 。希望它可以帮助:)

You may also have to take into account the possible ./ at the beginning of argv[0]. Hope it helps :)

这篇关于兼容Mac fopen函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆