Re: [Newbie] Basic file handling questions...
Re: [Newbie] Basic file handling questions...
- Subject: Re: [Newbie] Basic file handling questions...
- From: Chris Parker <email@hidden>
- Date: Thu, 8 Nov 2001 11:25:11 -0800
Hi Martin,
There are a few things you might need to do first...
On Thursday, November 8, 2001, at 07:09 AM, Martin-Luc Girard wrote:
What I'm trying to do is read from a plain text file containing 50
characters per line, and this, one line at a time...
First of all, am I doing this correctly when opening my file?
NSString *filePath= @"~/database.txt";
NSMutableString *fileContents= [[NSMutableString alloc]
initWithString: @""];
NSFileHandle *theFile;
theFile = [NSFileHandle fileHandleForReadingAtPath: filePath];
Close - What's happening here is that your program is looking in its
current working directory for a directory called "~" and then within
that a file called "database.txt" -
Since it's not finding it, you're getting back nil for theFile.
First, you'll have to expand the ~ out in the path:
NSString *expandedPath = [filePath stringByExpandingTildeInPath];
and then pass expandedPath along to [NSFileHandle
fileHandleForReadingAtPath:].
Now the file's opened.
[fileContents initWithCString:[theFile readDataToEndOfFile]];
definitely does not work...
Well, you've already sent an init message (in this case initWithString:)
once to fileContents. Here, you're sending another one
(initWithCString) to it. You only want to init once. But... you only
need the string if you've got the data - for the case where you want to
get the whole file, you can say
NSData *rawFileContentsAsNSData = [fh readDataToEndOfFile];
NSString *fileContents
if( rawFileContentsAsNSData ) {
fileContents = [[NSString alloc]
initWith
Data:rawFileContentsAsNSData encoding:[NSString
defaultCStringEncoding]];
}
(I'm using NSString here, since once I've got the data into the string,
I'm not going to be changing the string - I'll let NSFileHandle take
care of creating the NSData for me that's the right size).
When I'm done with the fileContents string, I should send it a release
message. I could also have said
fileContents = [[NSString alloc]
initWith
Data:rawFileContentsAsNSData encoding:[NSString
defaultCStringEncoding] autorelease];
Which will cause fileContents to be released at a later time.
How would I read a 50 character lines?
NSFileHandle allows you to read from the file using its
readDataOfLength: method as well -
NSData *one50CharacterLine = [fh readDataOfLength:50];
This returns up to 50 -bytes- - so these comments apply to ASCII text.
If your file is non-ASCII (Unicode, etc) then you'll have to adjust the
length you read accordingly.
Finally, to close the file... is the following correct?
[theFile closeFile];
Yes, but it does not deallocate the NSFileHandle object - for that you
need [theFile release]; afterward.
.chris
--
Chris Parker <email@hidden>
Cocoa Frameworks Engineer
Catapultum habeo. Nisi pecuniam omnem mihi dabis, ad caput tuum
saxum immane mittam.