我有一个名为“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");更多推荐
发布评论