Re: Trouble reading a binary file.
Re: Trouble reading a binary file.
- Subject: Re: Trouble reading a binary file.
- From: glenn andreas <email@hidden>
- Date: Mon, 13 Dec 2004 10:12:44 -0600
On Dec 13, 2004, at 9:51 AM, April Gendill wrote:
Below is the code I'm trying to use to read a binary file. I'm getting an empty string though.. Can any one shed some light on this?
FILE * fptr;
fptr = fopen([[panel filename]cString],"rb");
You should use [[panel filename] fileSystemRepresentation] anytime you pass an NSString to the BSD layer - if there are any non-ascii character in the path, cString will remove them, preventing this from working.
char chr;
NSMutableString * fd =[[NSMutableString alloc]init];
while(chr =getc(fptr) != EOF){
[fd appendString:[NSString stringWithFormat:@"%c",chr]];
}
Better, though, is to just do:
NSMutableString *fd = [[NSMutableString stringWithContentsOfFile: [panel filename]];
and let the system do all the work for you.
However, if you really want a binary file, you don't want to put it into a string, since a string holds "characters" (which can be encoded in the file as UTF8 or "the default C string encoding"). So for real binary data:
NSMutableData *fd = [[NSMutableData dataWithContentsOfFile: [panel filename]];
Glenn Andreas email@hidden
<http://www.gandreas.com/> oh my!
Mad, Bad, and Dangerous to Know
_______________________________________________
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