gpt4 book ai didi

ios - 当文件名包含 unicode(utf8) 字符时,ios 10.3 中的 fopen 出现问题

转载 作者:可可西里 更新时间:2023-11-01 05:43:02 25 4
gpt4 key购买 nike

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

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

最佳答案

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

因此需要使用高级 Foundation API:

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

...在那里填写数据

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

关于ios - 当文件名包含 unicode(utf8) 字符时,ios 10.3 中的 fopen 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43590236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com