简体   繁体   English

写入文件时丢失字节

[英]Losing bytes when file is written

I'm writing a NSData object to a file using this code: 我正在使用以下代码将NSData对象写入文件:

NSString *thePath = [[NSBundle mainBundle] pathForResource:@"cert" ofType:@"p12"];
NSData *data = [NSData dataFromBase64String:cert];
NSLog(@"Data length is %i", [data length]);
[data writeToFile:thePath atomically:YES]; //doesn't matter atomically YES or NO, same result

And it prints out 它打印出来

Data length is 3078 数据长度为3078

However when I read the retrieved file I'm missing 70 bytes! 但是,当我读取检索到的文件时,我缺少70个字节!

NSString *thePath = [[NSBundle mainBundle] pathForResource:@"cert" ofType:@"p12"];
NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath];
NSLog(@"PKCS12Data length is %i", [PKCS12Data length]);

Which prints out: 打印出:

PKCS12Data length is 3008 PKCS12数据长度为3008

So what is going on here? 那么这是怎么回事? Is there another way to create the file with all of its data? 还有另一种方法来使用其所有数据创建文件吗?

You can't write into the app bundle as it is read-only. 您无法将其写入应用捆绑包,因为它是只读的。 Write your cert.p12 files into the Documents folder. 将cert.p12文件写入Documents文件夹。 (I'd guess you already have a cert.p12 file in your app bundle and its size is 3008. Did you check the BOOL return from writeToFile:?) (我想您的应用程序捆绑包中已经有一个cert.p12文件,文件大小为3008。您是否检查了writeToFile的BOOL返回值??)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM