Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Detecting new line character(s) in text file?



On Nov 17, 2007, at 2:51 PM, Jean-Nicolas Jolivet wrote:

however..this only works if I have a text file that was saved on a mac... Is there an easy way to determine which new line character is used in a textfile (eighther linux, mac or win) and then use this for parsing afterward?

You'll have to do it heuristically. Probably the easiest method is to read the first kilobyte or so of a file, and scan for \r. If you find one, look at the next character and see if it's a \n. If so, it's a DOS-format (\r\n) file. If not, it's an old-style Mac (\r) text file. If you don't see a \r in the beginning of the file at all, assume it's a new-style Mac or Linux/Unix file (\n).


Another way would be to shell out (via NSTask) to the "file" command, which will tell you the kind of file (eg, "ASCII text, with CRLF line terminators"). However, that's fairly expensive, so I'd be careful if you're doing it frequently. Also, you'll have to parse file's output, and keep up with that if it changes.

--John

_______________________________________________

Cocoa-dev mailing list (email@hidden)

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/email@hidden

This email sent to email@hidden
References: 
 >Detecting new line character(s) in text file? (From: Jean-Nicolas Jolivet <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.