user7914103
user7914103

Reputation: 41

issue with fopen in ios 10.3 when file name has unicode(utf8) characters

I am trying to create a file called 'abcó.txt' in documents folder in iOS 10.3. fopen() is succeeding but file is not created. Any idea why? Following is my code.

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.");
    }
}

Upvotes: 4

Views: 434

Answers (1)

Dmytro Tregubenko
Dmytro Tregubenko

Reputation: 71

iOS 10.3 uses new file system, please take a look at APFS is currently unusable with most non-English languages

So need to use high-level Foundation APIs:

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

... fill data there

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

Upvotes: 3

Related Questions