c++ - 我可以在 IOS 上使用 tempnam 吗?

标签 c++ objective-c ios

我正在将 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/

相关文章:

c++ - Windows xp 上的 Mutex 给出 0xC0000096 : Privileged instruction exception

实现 Go goroutine 或 Go channel 的 C++ 库?

c++ - 将像 Strlen C++ 一样工作的函数

ios - UITableViewController 内的 UINavigationBar

ios - Swift 中的向上滑动动画

ios - 如何从触摸点呈现 ViewController?

c++ - 是否保证模板模板参数调用用户提供的推导指南

objective-c - CALayer 不显示

ios - 应用程序试图以模态方式呈现事件 Controller ios

ios - 以编程方式禁用麦克风