2017-04-24 70 views
3

我想創建一個名爲'abcó.txt'在iOS 10.3文檔文件夾中的文件。 fopen()成功,但文件沒有創建。任何想法爲什麼?以下是我的代碼。問題與fopen在ios 10.3當文件名有unicode(utf8)字符

void test_fopen(){ 
    const char *docsFolder = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] fileSystemRepresentation]; 
    std::string filePath = std::string(docsFolder) + "/abc"; 
    char oWithAcute[2] = {(char)0xC3, (char)0xB3, (char)0x00}; // the ó character to append 
    filePath = filePath + oWithAcute + ".txt"; 
    FILE *fp = fopen(filePath.c_str(), "wb"); 
    NSLog(@"Trying to create file using fopen: %s", filePath.c_str()); 
    if(fp != NULL){ 
     NSLog(@"fopen() SUCCEEDED."); 
    } 
    else { 
     NSLog(@"fopen() FAILED."); 
    } 
    fclose(fp); 

    NSFileManager* fm = [NSFileManager defaultManager]; 
    NSString *nsStr = [NSString stringWithUTF8String:filePath.c_str()]; 
    if (![fm fileExistsAtPath: nsStr]) { 
     NSLog(@"File is NOT actually created."); 
    } 
    else{ 
     NSLog(@"File is actually created."); 
    } 
} 

回答

2

iOS的10.3使用新的文件系統,請看看APFS is currently unusable with most non-English languages

因此需要使用高層次的API基金會:

NSFileManager *fm = [NSFileManager defaultManager]; 
NSMutableData *data = [NSMutableData data]; 

...填寫的數據有

[fm createFileAtPath:filePath contents:data attributes:nil];