我正在将 C++ 库移植到 iOS,但遇到了代码调用 tmpnam 的问题。该函数返回“var/tmp/tmp.0.0xGlzv”,我假设它在允许我的应用程序运行的“沙箱”之外。随后的 fopen 返回“不允许操作”。有可行的替代品吗?
最佳答案
关于
[NSTemporaryDirectory() stringByAppendingPathComponent:@"myTempFile1.tmp"];
?
对于唯一的文件名,尝试这样的事情:
NSString *uniqueTempFile()
{
int i = 1;
while (YES)
{
NSString *currentPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%i.tmp", i]];
if (![[NSFileManager defaultManager] fileExistsAtPath:currentPath])
return currentPath;
else
{
i++;
}
}
}
这很简单,但可能不是最节省内存的答案。
关于c++ - 我可以在 IOS 上使用 tempnam 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5379829/