我是IOS编程的新手。我想将一些数据写入文件。我已成功打开文档路径中的文件。但是 fwrite 它没有按预期工作。如果我打开文件它是空的。它是我正在使用的代码。我做错了什么。
typedef struct test {
int a;
} TEST_OBJ;
TEST_OBJ test_obj1;
test_obj1.a = 5;
TEST_OBJ *data_ptr = &test_obj1;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"MyFile1.txt"];
//This is working fine.
// [data writeToFile:appFile atomically:YES];
NSFileManager *fileManager = [[NSFileManager alloc]init];
char const *path = [fileManager fileSystemRepresentationWithPath:appFile];
FILE *fp = fopen(path, "w+b");
// This write is not working. File is empty.
int cnt = fwrite (data_ptr , 1 , sizeof(test_obj1) , fp );
fclose(fp);
fp = NULL;
任何人请告诉我我做错了什么?