我想通過fopen創建一個文件,然後寫它,但奇怪的事情發生。ios上fopen的奇怪行爲
- 當我插入iPhone到USB端口。一切正常。按預期在tmp目錄或文檔目錄中創建一個文件。
- 當我關閉設備並做同樣的事情時,文件沒有出現。我想知道爲什麼。
我使用fopen來創建文件。在我的情況下,我應該這樣做來創建並寫入文件。調用是fopen(pcm_output,「wb +」);
我想通過fopen創建一個文件,然後寫它,但奇怪的事情發生。ios上fopen的奇怪行爲
我使用fopen來創建文件。在我的情況下,我應該這樣做來創建並寫入文件。調用是fopen(pcm_output,「wb +」);
您需要使用此調用。
char const *path = [fileManager fileSystemRepresentationWithPath:url.path];
從文檔...
fileSystemRepresentationWithPath: - (爲const char *)fileSystemRepresentationWithPath:(的NSString *)路徑
的iOS(2.0和更高版本)
返回一個C給定路徑的字符串表示,用於正確編碼供文件系統使用的Unicode字符串。
path:包含文件路徑的字符串對象。 正確編碼供文件系統使用的Unicode字符串的路徑的C字符串表示。
謝謝,它的工作原理。我想知道爲什麼這個方法可行,爲什麼NSUTF8StringEncoding不起作用。 – seanxiaoxiao 2012-04-14 05:16:45
這將創建一個適當的編碼,但請記住,文件系統不喜歡任何字符。這就是爲什麼他們有一個特殊的方法來爲文件系統生成char *版本。 – 2012-04-14 06:15:33
您可能正在編寫沙箱之外,您可以發佈路徑嗎? 就像一個測試嘗試打開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+");
你好,我不得不寫文件此路徑下」 的/ var /移動/應用/ ED98116B-BD1E-4319-B473-A699DB6B9F86 /文檔「我相信它在沙箱裏面。無論如何,我會嘗試你的方法。 – seanxiaoxiao 2012-03-17 14:32:44
如果我的設備已經完成了監獄休息,這無關緊要嗎? – seanxiaoxiao 2012-03-17 14:39:55
另外,當我插入電纜時它工作正常。 – seanxiaoxiao 2012-03-17 14:43:45
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字符串,顯式爲const char * style,但問題依然如故。
@fbernardo代碼在這裏 – seanxiaoxiao 2012-03-18 14:27:42
這是代碼爲ios或本機系統插入? – Stavash 2012-03-17 11:53:05
我這麼認爲。或者即使插入 – seanxiaoxiao 2012-03-17 13:12:36
也無法工作我還得到了這個「3月17日21:42:45未知的CommCenter [23]:MessageCenterModel告訴PDP上下文-1去活動 Mar 17 21:42:45未知sandboxd [3933] :ZhiJia_Listen(3916)拒絕文件的寫入創建/ 6 「 –
seanxiaoxiao
2012-03-17 13:54:51