Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-22-2008, 12:19 PM   #1 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 2
ayrkain is an unknown quantity at this point
Default Text File opening

Haven't seen this mentioned elsewhere, but maybe you guys will know. Is there an easy way to open a text file in Obj. C? So far I've tried this little number:

[path = [NSBundle mainBundle] pathForResource:@"textfile" ofType:@"txt" inDirectory:@""];
FILE *pfile = fopen(path, "r");

Later I setText in a textView to pfile. What am I doing wrong here?
ayrkain is offline   Reply With Quote
Old 04-22-2008, 03:38 PM   #2 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 420
jeff_lamarche is an unknown quantity at this point
Send a message via AIM to jeff_lamarche Send a message via Yahoo to jeff_lamarche
Default Re: Text File opening

What exactly are you trying to do? I'm confused about your code - you can't have an assignment operator (=) inside of a message call the way you do - this code shouldn't even compile. Perhaps you meant:

NSString *path = [[NSBundle mainBundle] pathForResource:@"textFile" ofType:@"txt" inDirectory:@""];

Anyway, You certainly can use stdio calls to do your file management, but there are a number of easier ways to read files in Cocoa - most people don't use the low-level calls unless they need to do something rather unusual because then you are responsible for all sorts of things like endianness that would be handled automatically for you using one of the Cocoa objects.

You can use NSFileHandle for moderately complex situations where you need to seek to offsets or append data to a file.

It's pretty well documented here:

http://developer.apple.com/documentatio ... rence.html

If you just want to read data into memory, you can use something like:

NSError *error;
NSString *myString = [NSString stringWithContentsOfFile:@"/path/to/myfile.txt" encoding:NSUTF8StringEncoding error:*error];

or if it's not text data, you can do something similar with NSData

NSData *myData = [NSData dataWithContentsOfFile:@"/path/to/myfile.txt"];

Hope this helps,
Jeff
__________________
Check out my iPhone Dev Blog
You can send me e-mail at my forum username at mac dot com.
jeff_lamarche is offline   Reply With Quote
Old 04-22-2008, 03:53 PM   #3 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 2
ayrkain is an unknown quantity at this point
Default Re: Text File opening

You're right, I'm probably going way too low-level for this (I'm more acquainted with Perl than Obj. C). I really just want to display a text file. Thanks for the help.
ayrkain is offline   Reply With Quote
Old 04-22-2008, 03:59 PM   #4 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 420
jeff_lamarche is an unknown quantity at this point
Send a message via AIM to jeff_lamarche Send a message via Yahoo to jeff_lamarche
Default Re: Text File opening

No worries - Cocoa is big, you're not going to know where everything is right from the start.
__________________
Check out my iPhone Dev Blog
You can send me e-mail at my forum username at mac dot com.
jeff_lamarche is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting Directory File Contents RegularKid iPhone SDK Development 9 12-10-2009 12:09 AM
How to call Obj-c functions in C++ file? rick iPhone SDK Development 5 01-28-2009 11:35 AM
Upload XML File to Webserver jrgalan iPhone SDK Development 3 07-31-2008 09:57 AM
file saving question myersn024 iPhone SDK Development 8 06-15-2008 04:03 PM
Download RSS/XML file viet124 iPhone SDK Development 2 05-13-2008 10:09 AM


» Advertisements
» Online Users: 408
13 members and 395 guests
13dario13, 7twenty7, buggen, EvilElf, glenn_sayers, j.b.rajesh@gmail.com, LunarMoon, morterbaher, QuantumDoja, sacha1996, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,673
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, morterbaher
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:31 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0