fopen在ios上的怪异行为

时间:2021-09-11 09:29:15

I am trying to create a file by fopen and then write it, but weird things happened.

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

  1. When I plug in the iphone to the usb port. Everything works fine. A file is created at the tmp directory or the document directory as expected.
  2. 当我把iphone插入usb接口时。一切工作正常。在tmp目录或文档目录中创建一个文件。
  3. When I plug off the device and do the same thing, the file did not appear. I was wondering why.
  4. 当我关掉设备并做同样的事情时,文件没有出现。我想知道为什么。

I use fopen to create the file. In my case, I should do this to create and then write the file. The call is fopen(pcm_output, "wb+");

我使用fopen创建文件。在我的例子中,我应该这样创建文件,然后编写文件。调用为fopen(pcm_output,“wb+”);

3 个解决方案

#1


6  

You need to use this call.

你需要用这个电话。

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

From the docs...

从文档…

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

withpath: - (const char *)文件系统表示路径:(NSString *)路径。

iOS (2.0 and later)

iOS(2.0及以后)

Returns a C-string representation of a given path that properly encodes Unicode strings for use by the file system.

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

path: A string object containing a path to a file. A C-string representation of path that properly encodes Unicode strings for use by the file system.

路径:包含文件路径的字符串对象。路径的c字符串表示,它正确地编码了Unicode字符串,供文件系统使用。

#2


1  

You're probably writing outside of the sandbox, can you post the path? Just as a test try to turn on iTunes Sharing (this should have no effect, it's just a test) for your app.

你可能写在沙箱外面,你能贴出路径吗?就像一个测试试图打开iTunes分享(这应该没有效果,这只是一个测试)为你的应用。

EDIT:

编辑:

After testing I discovered that you have to use:

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

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

Instead of just:

而不是:

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

#3


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];

The original code is this, in which tts_resource tts_log pcm_output and wav_output are defined in a .h file and used in a .c file with fopen.

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

I had tried in your way to init the const string with the explicit const char* style, but the problem remains the same.

我尝试过用显式const char*样式初始化const字符串,但是问题还是一样的。

#1


6  

You need to use this call.

你需要用这个电话。

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

From the docs...

从文档…

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

withpath: - (const char *)文件系统表示路径:(NSString *)路径。

iOS (2.0 and later)

iOS(2.0及以后)

Returns a C-string representation of a given path that properly encodes Unicode strings for use by the file system.

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

path: A string object containing a path to a file. A C-string representation of path that properly encodes Unicode strings for use by the file system.

路径:包含文件路径的字符串对象。路径的c字符串表示,它正确地编码了Unicode字符串,供文件系统使用。

#2


1  

You're probably writing outside of the sandbox, can you post the path? Just as a test try to turn on iTunes Sharing (this should have no effect, it's just a test) for your app.

你可能写在沙箱外面,你能贴出路径吗?就像一个测试试图打开iTunes分享(这应该没有效果,这只是一个测试)为你的应用。

EDIT:

编辑:

After testing I discovered that you have to use:

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

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

Instead of just:

而不是:

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

#3


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];

The original code is this, in which tts_resource tts_log pcm_output and wav_output are defined in a .h file and used in a .c file with fopen.

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

I had tried in your way to init the const string with the explicit const char* style, but the problem remains the same.

我尝试过用显式const char*样式初始化const字符串,但是问题还是一样的。