|
#1
| |||
| |||
| how do I read text from a file into a string? Hi, I'm trying to do what seems like a very basic thing, but for some reason i can't figure it out. I've searched the web far and wide for a simple tutorial or example, and the best I could find was this: NSString *pathToFile = [@"~/Desktop/smallTextFile.txt" stringByExpandingTildeInPath]; NSString *contentsOfFile = [NSString stringWithContentsOfFile athToFile];printf("Contents of file: %s", contentsOfFile); It sort of works, because if I move the file, the printf is "NULL" rather than "\240{\246t", but besides that difference, I get: 2006-10-22 15:48:51.597 NetFlix_01[3416] *** _NSAutoreleaseNoPool(): Object 0x308340 of class NSCFString autoreleased with no pool in place - just leaking Contents of file: \240{\246t The documentation on how to setup the Autoreleasepool is more confusing than helpful, and its VERY long. Does anyone know a quick answer to the question? how do I load text from a file into a string?? http://developer.apple.com/documenta...tIO/TextIO.pdf This link sounds promising, it explains with example code how to create and object and a method to do it, but it doesnt show how to call it. For example, where do I enter the file name? and what line brings the text into a string that I can use? thanks, -Parker |
|
#2
| |||
| |||
| I figured out how to do it. I actually had it figured out before, (you were all probably laughing at me) but I didnt know how to confirm it. And I had to also figure out how to deal with the autorelease memory pool business, here is the code that works: NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; ...all the code that uses the string [pool release]; Thats a big FYI for you all. This line of code confirms that the string has the data from the file NSLog(@"%@", contents); I'm still having trouble with the NSScanner object, which I believe is the best way to get the data into variables for calculations. It seems to only scan the first variable. Does anyone have some sample code of an NSScanner working through a bunch of numbers??? Ideally I could just integers from the string one at a time, because there isn't much in the way of landmarks to "scanUpTo". |
|
#3
| ||||
| ||||
| You shouldn't actually need to set up an autorelease pool except in two cases: 1) This code is running in its own thread. 2) This code is running from the main event loop, not in any Cocoa-ish place. In either situation, the first thing you should do is to create the pool, and the last thing is to release it. As for NSScanner, it looks like you'd need to use a loop. Something like this (warning: completely untested): Code: int i;
while (![scanner isAtEnd]) {
if ([scanner scanInt:&i]) {
<do something with i here>;
}
} |
![]() |
| Thread Tools | |
|
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Text file problem | frendy | Software Programming & Web Scripting | 0 | July 15th, 2005 12:05 AM |
| How line-wrap a text string (need width/height in pixels)? | noleander | Software Programming & Web Scripting | 0 | July 13th, 2005 07:17 PM |
| Help with text file processing | aquamacus | Software Programming & Web Scripting | 12 | March 26th, 2005 03:51 PM |
| PC can't read Mac file | marshaque | Mac OS X System & Mac Software | 12 | October 2nd, 2004 01:02 PM |
| Help using grep to search for a text string | vikingshelmut | Unix & X11 | 4 | December 23rd, 2002 02:45 PM |