如何在iPhone上使用FILE *(How to use FILE * with iPhone)

编程入门 行业动态 更新时间:2024-10-12 03:21:05
如何在iPhone上使用FILE *(How to use FILE * with iPhone)

我有一个名为“0.ballpoint”的文件,我想要做的就是存储一些坐标(不要真的想使用Core Data,因为它看起来有点过分)。 我把它放在我的项目中但是当我尝试这样做时:

if ( access("0.ballpoint", F_OK) != -1) { printf("file exists\n"); } else { printf("doesn't exist\n"); }

它说它“不存在”。 我需要输入完整的路径名吗? 如果我把它放在真正的iPhone / iPod Touch上,我该怎么办?

I have a file called "0.ballpoint" that all I want to do is store some coordinates with (don't really want to use Core Data because it seem a little excessive). I placed it in my project but when I try doing this:

if ( access("0.ballpoint", F_OK) != -1) { printf("file exists\n"); } else { printf("doesn't exist\n"); }

It says it "doesn't exist". Do I need to put the full path name? And if I do what do I do when I place it on the actual iPhone/iPod Touch?

最满意答案

CoreData不适用于文件访问。 还有其他适用于iPhone的文件API。 请浏览NSFileManager和NSStream文档。

例如,如果您打算检查文件是否存在于特定路径,则可以使用(将文件添加到资源)

NSString *filePath = [[NSBundle mainBundle] pathForResource: @"0" ofType: @"ballpoint"]; BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];

除非您有正当理由这样做,否则不要使用低级别的东西(C文件句柄)。 要仔细选择路径(因为你不能在某个路径上做出假设,这可能会在将来停止存在)。

话虽如此,如果您更喜欢使用C文件处理,

NSString *filePath = [[NSBundle mainBundle] pathForResource: @"0" ofType: @"ballpoint"]; FILE *fileHandle = fopen([filePath cStringUsingEncoding:NSASCIIStringEncoding],"r");

CoreData is not meant for File access. There are other file API's available for iPhone. Please go through NSFileManager and NSStream documentation.

For example, if your intention is to check if file exists at certain path,you may use (add your file to resources)

NSString *filePath = [[NSBundle mainBundle] pathForResource: @"0" ofType: @"ballpoint"]; BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];

Prefer not to use low level stuff (C File handles) unless you have a valid reason to do so. Paths are to be carefully chosen (as you cant make assumptions on certain path, which might cease to exist in future).

Having said that, if you prefer to work with C file handling ,

NSString *filePath = [[NSBundle mainBundle] pathForResource: @"0" ofType: @"ballpoint"]; FILE *fileHandle = fopen([filePath cStringUsingEncoding:NSASCIIStringEncoding],"r");

更多推荐

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

发布评论

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

>www.elefans.com

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