Read a text file line by line?

Apprentice
Posts: 6
Joined: 2011.12
Post: #1
Is it possible to read a text file line by line or some way split up the lines once read out of a text file?

Thanks
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
Quote this message in a reply
Apprentice
Posts: 6
Joined: 2011.12
Post: #3
(Dec 13, 2011 10:02 PM)ThemsAllTook Wrote:  Yes. For example, http://www.opengroup.org/sud/sud1/xsh/fgets.htm

Thank you very much, I forgot to note that I'm using Objective C and this is for iOS.
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2010.02
Post: #4
If you can't get ThemsAllTook's answer to work I'm quite sure you can find what you are searching for here:
NSString Class Reference

Or by googling: "Cocoa file to string" & "NSString split into lines"

McSpider
Quote this message in a reply
Member
Posts: 164
Joined: 2010.10
Post: #5
if you know that the lines are all of the same length (suppose 30 chars) you can use fscanf("%30c",&string);
Quote this message in a reply
Apprentice
Posts: 6
Joined: 2011.12
Post: #6
I ended up figuring it out. I just used this:

NSArray *lines = [fileContent componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

Below it I loop through the array and separate it out into strings.
Quote this message in a reply
Member
Posts: 254
Joined: 2005.10
Post: #7
If your input file is large enough, you will also want to consider using an input stream buffer. This will allow you to read only a portion of the file so that you don't dump the entire file into memory at once.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Fast, 2d, line of sight? TimMcD 1 3,713 Dec 25, 2009 10:46 PM
Last Post: Skorche
  Applets from the Command Line merrill541 0 2,085 Nov 23, 2008 12:44 PM
Last Post: merrill541
  Command line arguments anthony 3 3,310 Dec 22, 2007 04:12 AM
Last Post: OneSadCookie
  fgets is only reading part of the line Joseph Duchesne 3 3,001 Feb 10, 2006 01:23 AM
Last Post: Taxxodium
  Multiple Defines on One Line unknown 10 5,809 Jul 31, 2005 01:21 PM
Last Post: OneSadCookie