首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当文件名包含unicode(utf8)字符时,iOS10.3中的fopen存在问题

当文件名包含unicode(utf8)字符时,iOS10.3中的fopen存在问题
EN

Stack Overflow用户
提问于 2017-04-24 22:03:22
回答 1查看 406关注 0票数 4

我正在尝试在iOS 10.3的documents文件夹中创建一个名为'abcó.txt‘的文件。fopen()成功,但未创建文件。知道为什么吗?以下是我的代码。

代码语言:javascript
复制
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.");
    }
}
EN

回答 1

Stack Overflow用户

发布于 2017-04-28 21:25:00

iOS 10.3使用了新的文件系统,请查看APFS is currently unusable with most non-English languages

因此需要使用高级Foundation API:

代码语言:javascript
复制
NSFileManager *fm = [NSFileManager defaultManager];
NSMutableData *data = [NSMutableData data];

..。在那里填充数据

代码语言:javascript
复制
[fm createFileAtPath:filePath contents:data attributes:nil];
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43590236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档