gpt4 book ai didi

iphone - Object-C调用C函数读取文件

转载 作者:太空宇宙 更新时间:2023-11-04 08:57:26 26 4
gpt4 key购买 nike

我正在处理一个 iPhone 项目,我需要调用一些 C 函数,这些函数位于 C 文件(如 myfile.c)中。我可以调用其中的一些函数。但是当我需要在C文件中打开一个文件时。我遇到了访问错误(gdb)。我要读取的文件已经放到工程文件夹下,在工程中导入。甚至,我不能用 C 函数创建新文件。任何人都可以帮助我吗?非常感谢。

最佳答案

在 iOS 中对文件使用 C 函数时,你不能这样做

readFunction("filename.txt");

你必须为此创建路径,例如你的包中的文件

readFunction([[NSBundle mainBundle] pathForResource:@"filename" ofType:@"txt"].UTF8String);

例如写作也是如此

NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [docsDir stringByAppendingPathComponent:@"filename.txt"];
writeFunction(filePath.UTF8String);

关于iphone - Object-C调用C函数读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16268111/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com