打开文件在Mac OS X

编程入门 行业动态 更新时间:2024-10-09 04:16:18
本文介绍了打开文件在Mac OS X的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在Mac OS X打开用C的文本文件+ +,但我总是得到一个总线错误。

我不在乎把文件。我只需要读它。我写的地址错了吗?或总线错误有另一个原因呢?

FILE * DIC;DIC = FOPEN(翻译字典,RB);DIC = FOPEN(./ DICT,RB);DIC =的fopen(〜/ DICT,RB);DIC =的fopen(〜// DICT,RB);

解决方案

通过澄清一点我看在C code(不是C ++!)问题是, fopen()函数收益 NULL 。您可以检查这个问题到底是怎样通过报告详细的错误:

IF((DIC = FOPEN(翻译字典,RB)== NULL){    fprintf中(标准错误,%S \\ n,PERROR(ERROR));    出口(1);}

如果 fopen()函数无法找到该用户的桌面上的文件和你希望你的code在多个平台上工作,那么你可以定义一个函数来获取与使用的fopen用户的桌面目录()。类似

的char * user_desktop(的char * buf中,为size_t LEN){    为const char * const的DESKTOP_DIR =#IFDEF PC    C:\\\\ Documents和Settings \\\\ \\\\ Pooya桌面\\\\#elif指令定义(OSX)    /用户/ Pooya /桌面/#elif指令定义(LINUX)    /家庭/用户/ pooya /桌面///失败,如果没有指定OS编译...#万一    返回函数strncpy(BUF,DESKTOP_DIR,LEN);}

您可能要考虑让桌面为每个操作系统的路径更为可靠的方法。大多数的操作系统都有一个这样的API,所以做你的研究。也有分裂的行为更稳健的方式为各种平台,你可以看看这或打开约一个不同的问题。我只是想给前preSS我的想法,其具有将返回你在哪个平台上编译code相应的桌面路径不管一个功能。

I am trying to open a text file with C++ in Mac OS X but I always get a Bus error.

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");

解决方案

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:

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

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.

更多推荐

打开文件在Mac OS X

本文发布于:2023-06-02 01:04:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/431011.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   Mac   OS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!