macos 在 Mac OS X 中打开文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2615492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opening a file in Mac OS X
提问by Pouya BCD
I am trying to open a text file with C++ in Mac OS X but I always get a Bus error.
我试图在 Mac OS X 中用 C++ 打开一个文本文件,但我总是收到总线错误。
I do not care where to put the file. I just need to read it. Am I writing its address wrong? or that Bus Error has another reason?
我不在乎把文件放在哪里。我只需要阅读它。我的地址写错了吗?或者总线错误有另一个原因?
FILE *dic;
dic = fopen("DICT","rb");
dic = fopen("./DICT","rb");
dic = fopen("~/DICT","rb");
dic = fopen("~//DICT","rb");
回答by wilhelmtell
With a little bit of clarification I see the problem in your C code (not C++!) is that fopen()
returns NULL
. You can check what the problem really is by reporting the detailed error:
稍微澄清一下,我发现您的 C 代码(不是 C++!)中的问题是fopen()
返回NULL
. 您可以通过报告详细错误来检查问题的真正原因:
if( (dic = fopen("DICT", "rb") == NULL ) {
fprintf(stderr, "%s\n", perror("ERROR:"));
exit(1);
}
If fopen()
fails to find the file on the user's desktop and you wish your code to work on multiple platforms then you might define a function to get the user's desktop directory for using with fopen()
. Something like
如果fopen()
在用户桌面上找不到该文件并且您希望您的代码在多个平台上工作,那么您可以定义一个函数来获取用户的桌面目录以使用fopen()
. 就像是
char* user_desktop(char* buf, size_t len)
{
const char* const DESKTOP_DIR =
#ifdef PC
"C:\Documents and Settings\Pooya\Desktop\"
#elif defined(OSX)
"/Users/Pooya/Desktop/"
#elif defined(LINUX)
"/home/users/pooya/Desktop/"
// fail to compile if no OS specified ...
#endif
return strncpy(buf, DESKTOP_DIR, len);
}
You probably want to look into a more robust way of getting the path of the desktop for each operating system. Most operating systems have an API for this, so do your research. There are also more robust ways of splitting behaviour for various platforms, you can look into that or open a different question about that. I just wanted to express my idea, of having a function which will return you the appropriate desktop path no matter on which platform you compile your code.
您可能想要寻找一种更可靠的方法来获取每个操作系统的桌面路径。大多数操作系统都有用于此的 API,因此请进行研究。还有更强大的方法可以为各种平台拆分行为,您可以研究它或提出一个不同的问题。我只是想表达我的想法,无论您在哪个平台上编译代码,都有一个函数可以返回适当的桌面路径。
回答by user3727755
This code is correct! Pay attention to the directory where the executable is located. For sure the directory of the execution is not the same as you are expecting (I suppose, the directory of the .c
files, right?).
这段代码是正确的!注意可执行文件所在的目录。当然,执行的目录与您期望的不同(我想是.c
文件目录,对吗?)。
I believe you are executing the app from the IDE. This is commom in Xcode, it mounts the exec`s in another location than that where the project files are located, and this such location that is considered when you execute the program, whether you execute it from the IDE or not!
我相信您正在从 IDE 执行该应用程序。这在 Xcode 中很常见,它会将 exec 挂载在项目文件所在位置以外的其他位置,并且无论您是否从 IDE 执行程序,执行程序时都会考虑这个位置!
Simply move the file you want to read to the location of the application and it will work properly.
只需将您要读取的文件移动到应用程序的位置,它就会正常工作。