Re: writing & reading a file in Cocoa
Re: writing & reading a file in Cocoa
- Subject: Re: writing & reading a file in Cocoa
- From: Pierre-Olivier Latour <email@hidden>
- Date: Wed, 25 Dec 2002 19:11:00 -0800
Assuming you have:
typedef struct {
int a;
short b;
} Struct;
Struct myStruct;
You could do:
NSData* data = [NSData dataWithBytes:&myStruct length:sizeof(Struct)];
[data writeToFile:somePath atomically:YES];
Note that you could replace the first line with:
[NSData dataWithBytesNoCopy:&myStruct length:sizeof(myStruct)
freeWhenDone:NO];
To read back the data, simply do this:
NSData* data = [NSData dataWithContentsOfFile:somePath];
bcopy([data bytes], &myStruct, sizeof(Struct));
This is likely not the best approach for your specific case, but it'll give
you the idea :)
>
Happy holidays:
>
>
I am writing a preference pane. I want to read & write a C data
>
structure into a file. Is there an example of this anywhere?
>
>
I prefer not to use NSDictionary and instead read & write the whole C
>
structure as a block of data bytes.
>
>
Any help is appreciated.
>
>
Manooch Khajeh
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_____________________________________________________________
Pierre-Olivier Latour email@hidden
Palo Alto, USA
http://www.pol-online.net
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.