• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Slice a .txt file in lines
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Slice a .txt file in lines


  • Subject: Re: Slice a .txt file in lines
  • From: Ben Lachman <email@hidden>
  • Date: Tue, 20 Jun 2006 02:11:52 -0400

Check out NSString's text functions

<http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ Classes/NSString_Class/Reference/Reference.html#//apple_ref/doc/c_ref/ NSString>

particularly componentsSeparatedByString: is useful for splitting files by line endings.

->Ben

On Jun 20, 2006, at 1:44 AM, Neto wrote:

Hi all,
Another newbie question...
I need to import a .txt file to my app.
Each line of the .txt must be assigned to a key of an array.
With my little knowledge and some Google, I managed to do this:

-(IBAction)importFileMethod:(id)sender
{
NSString *path = @"/Users/neto/Desktop/import.txt";
NSError *error;
NSString *stringFromFileAtPath = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
NSString *cN, *sN; //(and so on - an NSString for each key)
MyArray *newItem = [[MyArray alloc] init];
cN=[stringFromFileAtPath substringWithRange: NSMakeRange(3,19)];
sN=[stringFromFileAtPath substringWithRange: NSMakeRange(20,25)];
[newItem setValue:cN forKey:@"keyOne"];
[newItem setValue:sN forKey:@"keyTwo"];
[arrayController addObject:newItem];
[newItem release];
}

The Problem: I can't use NSMakeRange(x,y) because in the different .txt files the lengths will vary. So I need a method that detects the beginning and the end of each line (each line ends with an Enter)


Any ideas?
_______________________________________________
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


  • Follow-Ups:
    • Re: Slice a .txt file in lines
      • From: Andreas Mayer <email@hidden>
References: 
 >Slice a .txt file in lines (From: Neto <email@hidden>)

  • Prev by Date: Slice a .txt file in lines
  • Next by Date: Re: Problem in simulating the events
  • Previous by thread: Slice a .txt file in lines
  • Next by thread: Re: Slice a .txt file in lines
  • Index(es):
    • Date
    • Thread