fosn在ios上的奇怪行为

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

  1. 当我将iphone插入USB端口时。 一切正常。 按预期在tmp目录或文档目录中创建文件。
  2. 当我插上设备并执行相同操作时,文件没有出现。 我想知道为什么。

我使用fopen来创建文件。 在我的情况下,我应该这样做来创建然后写入文件。 调用是fopen(pcm_output,“wb +”);

你需要使用这个电话。

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

来自文档……

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

iOS(2.0及更高版本)

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

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

你可能在沙箱之外写作,你可以发布路径吗? 正如测试尝试打开iTunes应用程序的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+"); 
 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字符串,但问题仍然是一样的。