Using NSData to read bytes from a file
Using NSData to read bytes from a file
- Subject: Using NSData to read bytes from a file
- From: Phil Faber <email@hidden>
- Date: Mon, 17 Apr 2006 21:56:43 +0100
- Resent-date: Wed, 19 Apr 2006 15:11:48 +0100
- Resent-from: Phil Faber <email@hidden>
- Resent-message-id: <email@hidden>
- Resent-to: Cocoa-dev List <email@hidden>
I've asked this before and received some guidance (thank you) but my
poor newbie brain doesn't understand the guidance! Some of the
guidance received was:
NSError *err; // this takes from you the complete burden of...
NSData *d=[NSData dataWithContentsOfFile:yourfilename options:0
error:&err];
if (!d) [NSApp presentError:err]; // ... reporting possible reading
errors
else {
unsigned char *bytes=[d bytes];
// you SHOULD check [d length] here if long enough!
[statusText setIntValue:bytes[8]];
...
I'm TRYING to find answers myself by reading the documentation but
there's SO much and it's hard to know where to start.
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?
I guess the problem is something to do with using wrong data types
but don't know what should be what!
Hopefully once someone can get the code working I can the analyse
that working code to figure out WHY it works!
Thanks.
Phil
___________________________________________________________
Switch an email account to Yahoo! Mail, you could win FIFA World Cup tickets. http://uk.mail.yahoo.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