• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: writing & reading a file in Cocoa
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

References: 
 >writing & reading a file in Cocoa (From: Manooch Khajeh <email@hidden>)

  • Prev by Date: Converting an NSScrollView decendant to NSView
  • Next by Date: (Somewhat OT) What is Image Capture doing?
  • Previous by thread: writing & reading a file in Cocoa
  • Next by thread: Re: writing & reading a file in Cocoa
  • Index(es):
    • Date
    • Thread