file i/o help

zots

Re-member
in my app i need to read/write data to some sort of preference file. NSFileHandle seems to be what i need. below i am trying to simply read all the data of the file into an NSData. the second line instantiates the NSFileHandle w/o error, but i am unsure of what the object/receiver(xxxxx) would be when i call readDataToEndOfFile.
Code:
NSData *aData;
[NSFileHandle fileHandleForReadingAtPath:@"/Applications/Test/Test.txt"];
aData = [xxxxx readDataToEndOfFile];
TIA
 
with a lucky search for NSFileHandle on yahoo i found some code and figured it out.
Code:
NSFileHandle *aFile;
NSData *aData;
aFile = [NSFileHandle fileHandleForReadingAtPath:@"/Applications/test.txt"];
aData = [aFile readDataToEndOfFile];
now i that i have the file data stored in aData, how do i convert it to a string, integer, etc?
 
very simple:

Code:
myString = [[[NSString alloc] initWithData:aData encoding:NSASCIIStringEncoding] autorelease];

the tricky part is that you have to know about the encoding of the text.

and a small warning: i've been trying to track down an error related with filehandles and it turned out that i had forgotten to close it in one rare condition -- still, in a totally different location in my app, i couldn't save anymore. seemed unrelated, but as soon as i closed the filehandle, everything was ok.
 
Back
Top