• 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: to write to file except NSData*
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: to write to file except NSData*


  • Subject: Re: to write to file except NSData*
  • From: Jens Alfke <email@hidden>
  • Date: Tue, 15 Apr 2008 14:48:01 -0700


On Apr 15, 2008, at 10:38 AM, Nick Rogers wrote:

but i want to write the length of (NSData *)data, before I write it to file because I will be writing a lot of NSData to the file,
so that I will be able to read the length first and then read the following NSData.
SO how can I write the length which is an integer to file before I write the NSData.

Don't use NSFileHandle, then. Use NSOutputStream, open it on the file (I don't think you have to create the file beforehand) and use its - write:maxLength: method to write whatever bytes you want.


(Though to be honest, I don't ever do this. If I want to read and write bytes from raw files like that, I just use the standard C APIs like fopen, fread, fwrite...)

But are you really sure you need to roll your own file format and I/O code for this? It's annoying code to write, it has to deal with byte- ordering issues, and bugs in your code will often have nasty side effects like corrupting files or even allowing a maliciously-crafted file to take over your app. Why not just collect the NSDatas in an NSArray and call -writeToFile: on it? Then you could be done with the whole task in two minutes.

—Jens

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >to write to file except NSData* (From: Nick Rogers <email@hidden>)
 >Re: to write to file except NSData* (From: Jeff LaMarche <email@hidden>)

  • Prev by Date: Re: binary search trees & binning
  • Next by Date: Re: [OT: security] smbclient
  • Previous by thread: Re: to write to file except NSData*
  • Next by thread: Using the debugger with local variables was Re: is this badly written code?
  • Index(es):
    • Date
    • Thread