Re: Reading a plain text file
Re: Reading a plain text file
- Subject: Re: Reading a plain text file
- From: Robert Dell <email@hidden>
- Date: Sun, 22 Jan 2006 08:48:14 -0500
Chad Armstrong wrote:
I have a plain text file I'm trying to read, and I want to place its
contents into an object, such as an NSString. For something which is
probably fairly trivial, I'm just not finding the answer I'm looking
for. I've worked some with NSArchiver and NSUnarchiver, but that seems
to work a little better with data saved in an XML format, while the
file I'm trying to read is just a plain text file (no XML formating or
anything of the sorts). What am I missing here? Thanks.
Chad
[myScriptName setString: [NSMutableString stringWithString: myscript]];
[pathToScripts setString: [self getScriptLocationUser: [myScriptName stringByAppendingString: @".txt"]]];
NSMutableString *scriptData = [[[NSMutableString alloc] init] autorelease];
long position = 0;
[scriptData setString: [NSString stringWithContentsOfFile: pathToScripts]];
// translate MSDOS text files to osx/unix text files
ignoreReplaceResults = [scriptData replaceOccurrencesOfString: @"\r\n" withString: @"\n" options: NSCaseInsensitiveSearch range: NSMakeRange(0, [scriptData length])];
// translate mac os 9 text files to osx/unix text files
ignoreReplaceResults = [scriptData replaceOccurrencesOfString: @"\r" withString: @"\n" options: NSCaseInsensitiveSearch range: NSMakeRange(0, [scriptData length])];
// get rid of tabs
ignoreReplaceResults = [scriptData replaceOccurrencesOfString: @"\t" withString: @" " options: NSCaseInsensitiveSearch range: NSMakeRange(0, [scriptData length])];
and this traverses folders to find the proper path if the file isn't precisely there
-(NSString *)getScriptLocationUser: (NSString *)whichScript;
{//myCharacter
NSMutableString *pathToFiles = [[[NSMutableString alloc] init] autorelease];
NSMutableString *scriptFileName = [[[NSMutableString alloc] init] autorelease];
[scriptFileName setString: whichScript];
[pathToFiles setString: [self locateFile: [NSMutableString stringWithString: scriptFileName] atPath: [NSMutableString stringWithString: [@"~/Documents/Simutronics/Scripts/" stringByAppendingString: myCharacter]]]];
NSLog(pathToFiles,nil);
return [NSString stringWithString: pathToFiles];
};
_______________________________________________
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