1

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?

Feroz
  • 699
  • 5
  • 17
  • 25
  • You just want to write one thing? Stick to Obj-C...there are convenience methods on NSString and NSData to write to files. – borrrden Jul 26 '12 at 09:52
  • I'm little bit puzzled how to write structure using NSData. Can you give me some suggestion? – Feroz Jul 26 '12 at 09:58
  • I'm not sure you have permission to write any files into the sandbox via `fwrite(...);`... – holex Jul 26 '12 at 10:18

4 Answers4

3

I recommend you use NSData instead of C functions.

NSData *myData = [[NSData alloc] initWithBytes:bytes length:length];
[myData writeToFile:path atomically:YES];
DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • Ya I tried with NSData, It worked fine. However is it possible to write a structure with that? – Feroz Jul 26 '12 at 09:53
  • Define a method to convert it to a string for writing. – borrrden Jul 26 '12 at 09:57
  • Or you could convert it to a NSDictionary and then write that to disk. – DrummerB Jul 26 '12 at 09:58
  • Or even just `NSData *data = [NSData dataWithBytes:&test_obj1 length:sizeof(TEST_OBJ)];` – borrrden Jul 26 '12 at 10:00
  • Also @DrummerB that write method has been deprecated since iOS 2.0 so you should use `writeToFile:options:error:` instead. – borrrden Jul 26 '12 at 10:03
  • Did you pass the correct length? Try debugging and see if the values you are passing are actually what you think they are. – DrummerB Jul 26 '12 at 10:16
  • Ya I did checking the length even hot coded the length exactly. I think problem is with structure. Becoz for the same api if I pass char buff it works perfectly. I doubting Is it possible to write structure in Obj C. Writing a String its more work for me Becoz I have client code which will give structure object. Thats why I tried with C I/O. – Feroz Jul 26 '12 at 10:30
  • It's possible to write structs too, you just have to pay attention at the [layout](http://stackoverflow.com/questions/2748995/c-struct-memory-layout). – DrummerB Jul 26 '12 at 10:37
  • @FerozMohideen Did you confirm that the data was created? Can you NSLog it? – borrrden Jul 26 '12 at 13:38
3

you can use NSData,NSArray,NSDictionary,they have [writeToFile: atomically:] method ,it can write the data into the file.

if you must C to read and write filestream,you can use my method below , it can add arrayString to the file .

- (void)putArrayString:(NSString *)arrayString toFilePath:(NSString *)filePath
{
    FILE *fileStream = fopen([filePath UTF8String], "a+");

    if(fileStream == NULL)
    {
        fclose(fileStream);
        fileStream = fopen([filePath UTF8String], "w+");
        fputs([arrayString UTF8String], fileStream);
    }
    else
    {
        fputs([arrayString UTF8String], fileStream);
    }
    fclose(fileStream);
}
cloosen
  • 993
  • 7
  • 17
0

I have changed the mode of opening a+ its writing and reading properly now. Thanks all for your help.

Feroz
  • 699
  • 5
  • 17
  • 25
-1

See if you can use this code:

https://github.com/RIKSOF/three20/blob/master/src/extThree20RemoteObject/Source/TTObjectModel.m

Start reading from the encodeToDocument method. It writes all properties of an object in to a JSON or XML file.

Any subclass of this class having any property is properly written back.

Khurram Ali
  • 835
  • 12
  • 18