Re: Using NSData to read bytes from a file
Re: Using NSData to read bytes from a file
- Subject: Re: Using NSData to read bytes from a file
- From: Nick Zitzmann <email@hidden>
- Date: Wed, 19 Apr 2006 08:29:26 -0600
On Apr 17, 2006, at 2:56 PM, Phil Faber wrote:
Can some kind person please re-write (not just explain the
problem), as required, my current code:
NSOpenPanel *panel = [NSOpenPanel openPanel];
NSString *inputFile = [panel filename];
NSData *fileContents = [NSData dataWithContentsOfFile:inputFile];
oneByte = [fileContents subdataWithRange:NSMakeRange(8,1)];
NSLog(@"%i",oneByte);
...which successfully prompts the user to select a file (of ANY
type - not just text) but displays (via NSLog) the number
"3744544" (which presumably is a pointer address).
...into something that will display (again via NSLog) the ASCII
value of character 8 in the file?
Instead of using -subdataWithRange: you probably want the data in a C
primitive rather than an NSData object, so try this instead:
uint8_t oneByte;
[fileContents getBytes:&oneByte range:NSMakeRange(8, 1)];
Then oneByte will contain the byte that's eight bytes into the file.
You might also want to verify that the data object is long enough; -
getBytes:range: will throw an NSRangeException if the location
supersedes the data object's length.
Nick Zitzmann
<http://www.chronosnet.com/>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden