I am new to IOS programming. I want to write some data into file. I have opened successfully the file in Document path. But fwrite its not working as expected. If I open the file its empty. Its my code I'm using. What I'm doing wrong.
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;
Anyone pls tell me what I'm doing wrong?