image
image

Go Back   macosx.com > Design, Media, Programming & Scripting > Software Programming & Web Scripting

Reply
 
Thread Tools
  #1  
Old October 22nd, 2006, 11:32 PM
Registered User
 
Join Date: Oct 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
parkimedes is on a distinguished road
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 stringWithContentsOfFileathToFile];
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
Reply With Quote
  #2  
Old October 27th, 2006, 03:04 AM
Registered User
 
Join Date: Oct 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
parkimedes is on a distinguished road
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".
Reply With Quote
  #3  
Old October 27th, 2006, 01:42 PM
Mikuro's Avatar
Crotchety UI Nitpicker
 
Join Date: Mar 2005
Posts: 2,504
Thanks: 4
Thanked 15 Times in 14 Posts
Mikuro is on a distinguished road
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>;
     }
}
__________________
Mac mini — 1.25GHz G4, 1GB RAM — OS 10.5.5

Useful programs: Privoxy, Butler, ffmpegX, VLC, Perian, Tofu, Wcalc
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump

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


All times are GMT -5. The time now is 01:52 PM.


Mac Support® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2000-2008 DigitalCrowd, Inc.