Re: Reading one line at a time using NSFileHandle



Tried that approach too. Problem with using NSString is that when it comes time
to write the line out to the new file, I need an NSData and to create one, I
have to use NSString's getBytes or getCharacters. Those only return unicode
arrays, which, when I go to write to a text file gives me a '?' between each
character. My original file is encoded in MacRoman. There is no way to specify
the encoing for the lines being written out.

It is absolutely mind-boggling that Apple does not provide a simple, easy API
for doing this.

Erg




________________________________
From: Jean-Daniel Dupas <devlists@xxxxxxxxxxxxx>
To: Erg Consultant <erg_consultant@xxxxxxxxx>
Cc: cocoa-dev@xxxxxxxxxxxxxxx
Sent: Thursday, March 26, 2009 2:05:55 PM
Subject: Re: Reading one line at a time using NSFileHandle

If this is a small file, just create an NSString and iterate over lines.
Here is a simple snippet that count number of lines in a string. You can modify
it for your purpose.

NSString *string;
unsigned numberOfLines, index, stringLength = [string length];
for (index = 0, numberOfLines = 0; index < stringLength; numberOfLines++)
index = NSMaxRange([string lineRangeForRange:NSMakeRange(index, 0)]);


Le 26 mars 09 à 21:27, Erg Consultant a écrit :

> I read the CocoaBuilder thread to no avail. fgets won't work in my case
> because my text file contains special characters which fgets mangles.
>
> BTW, my file is not large - less than 1K. And I know it's encoding -
> MacRoman. And I know the line endings "/n".
>
> But what I don't know is which API to use to read non-ASCII lines of text one
> at a time from one file, and write them to another file after processing each
> one.
>
> One would think Cocoa would have something like fgets but that works with any
> encoding.
>
> Erg
>
>
>
>
> ________________________________
> From: Matt Neuburg <matt@xxxxxxxxxxx>
> To: Erg Consultant <erg_consultant@xxxxxxxxx>
> Cc: cocoa-dev@xxxxxxxxxxxxxxx
> Sent: Thursday, March 26, 2009 7:24:29 AM
> Subject: Re: Reading one line at a time using NSFileHandle
>
> On Wed, 25 Mar 2009 19:36:01 -0700 (PDT), Erg Consultant
> <erg_consultant@xxxxxxxxx> said:
>> Is there a way to read one line of a text file at a time using NSFileHandle
> (the way fgets does)?
>
> Is it my imagination or did we not just have a big thread about this...?
>
> <http://www.cocoabuilder.com/archive/message/cocoa/2009/2/2/229302>
>
> m.
> --matt neuburg, phd = matt@xxxxxxxxxxx, <http://www.tidbits.com/matt/>
> A fool + a tool + an autorelease pool = cool!
> AppleScript: the Definitive Guide - Second Edition!
> http://www.tidbits.com/matt/default.html#applescriptthings
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (Cocoa-dev@xxxxxxxxxxxxxxx)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/cocoa-dev/devlists%40shadowlab.org
>
> This email sent to devlists@xxxxxxxxxxxxx
>




_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@xxxxxxxxxxxxxxx)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/maillists%40codeha.us

This email sent to maillists@xxxxxxxxx



Privacy