fosn在ios上的奇怪行为

时间:2012-03-17 10:50:49

标签: objective-c c ios

我正在尝试通过fopen创建一个文件然后编写它,但发生了奇怪的事情。

  1. 当我将iphone插入USB端口时。一切正常。将按照预期在tmp目录或文档目录中创建文件。
  2. 当我关闭设备并执行相同操作时,文件没有出现。我想知道为什么。
  3. 我使用fopen来创建文件。在我的情况下,我应该这样做来创建然后写文件。调用是fopen(pcm_output,“wb +”);

3 个答案:

答案 0 :(得分:6)

你需要使用这个电话。

    char const *path = [fileManager fileSystemRepresentationWithPath:url.path];

来自文档...

fileSystemRepresentationWithPath: - (const char *)fileSystemRepresentationWithPath:(NSString *)path

iOS(2.0及更高版本)

返回给定路径的C字符串表示形式,该路径正确编码Unicode字符串以供文件系统使用。

path:包含文件路径的字符串对象。 正确编码Unicode字符串以供文件系统使用的路径的C字符串表示。

答案 1 :(得分:1)

你可能在沙箱之外写作,你可以发布路径吗? 正如测试尝试打开iTunes分享(这应该没有效果,这只是一个测试)为您的应用程序。

修改

经过测试,我发现你必须使用:

NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [docsPath stringByAppendingPathComponent:[NSString stringWithCString:pcm_output encoding:NSUTF8StringEncoding]];    
fopen([filePath UTF8String], "wb+");

而不仅仅是:

fopen([filePath UTF8String], "wb+");

答案 2 :(得分:0)

NSArray *paths = NSSearchPathForDirectoriesInDomains(
                                                 NSDocumentDirectory, 
                                                 NSUserDomainMask, YES
                                                 ); 
NSString* docDir = [paths objectAtIndex:0];
_tempLogPath = [docDir stringByAppendingPathComponent: @"Aisound5_CBLog.log"];
_tempPcmPath = [docDir stringByAppendingPathComponent: @"OutPcm.pcm"];
_tempWavPath = [docDir stringByAppendingPathComponent: @"OutWav.wav"];


tts_resource = [[bundle pathForResource:@"Resource_dev" ofType:@"irf"] UTF8String];
tts_log = [_tempLogPath UTF8String];
pcm_output = [_tempPcmPath UTF8String];
wav_output = [_tempWavPath UTF8String];

原始代码是这样的,其中tts_resource tts_log pcm_output和wav_output在.h文件中定义,并在带有fopen的.c文件中使用。

我曾尝试过使用显式const char *样式初始化const字符串,但问题仍然存在。