Re: Opening old text files with non-deprecated stringWithContentsOfFile calls?

Michael Dupuis <smokinmonkey-/[email protected]> Tue, 12 Oct 2010 07:44:57 -0700 (PDT)
Newsgroups gmane.comp.macosx.devel
Message-ID <[email protected]>
Hi Andy,

Any of the files downloaded here will cause issues: http://www.grassrootsrecipes.com/

When I open them in TextWrangler, it appears that these are "Western  (MacOS) with Windows (CRLF), at least that is what's displayed at the bottom. If I just download one and try and read it in even using the

+ (id)stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error

call, it will fail. If I fallback to using the deprecated call, I can open it, so the deprecated call is doing SOMETHING that works, I just haven't been able to figure out what that is.

Michael

--- On Mon, 10/11/10, Andy Lee <[email protected]> wrote:

> From: Andy Lee <[email protected]>
> Subject: Re: Opening old text files with non-deprecated stringWithContentsOfFile calls?
> To: "Michael Dupuis" <smokinmonkey-/[email protected]>
> Cc: [email protected]
> Date: Monday, October 11, 2010, 10:10 AM
> Michael,
> 
> Can you post some links to such files so we can
> experiment?
> 
> Also, do you know what encoding the files are actually
> using, or at least intending to be using, and do the new
> calls work if you specify that encoding explicitly?  I
> wonder if the deprecated call succeeds because it guesses
> the encoding better, or it guesses the encoding incorrectly,
> or is less strict with incorrectly encoded files.
> 
> Does TextWrangler guess the encoding correctly?  If
> you dirty the file without actually changing it, and save
> the file with TextWrangler without changing the encoding,
> does the resulting file match the original byte for byte?
> 
> --Andy
> 
> On Oct 11, 2010, at 9:50 AM, Michael Dupuis wrote:
> > Greetings,
> > 
> > The + (id)stringWithContentsOfFile:(NSString *)path
> call in NSString has been deprecated a while, in favor of
> the newer calls that either have you specify or that can try
> and figure out the encodings of files when loading.
> > 
> > However, I've noticed that it's very easy to download
> older text files from the web that fail to import with an
> "Error Domain=NSCocoaErrorDomain Code=264
> UserInfo=0x14c1d9a0 "The file “file.txt” couldn’t be
> opened because the text encoding of its contents can’t be
> determined." error or similar.
> > 
> > The deprecated call opens these files without a
> problem. I tried various ways to open them, using NSData,
> etc. but unless you open them in something like TextWrangler
> and save them with a specific encoding, they all seem to
> fail to load.
> > 
> > Has anyone figured out what needs to be done to open
> files the way the deprecated call does? There must be some
> fallback when there is no encoding that can be done, but I
> can't seem to figure out what that is... Are people just
> basically forced to use files with specified encodings now?
> > 
> > Thanks, Michael
> 
> 
>