• 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: Reading/Writing Text files
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reading/Writing Text files


  • Subject: Re: Reading/Writing Text files
  • From: Douglas Davidson <email@hidden>
  • Date: Mon, 11 Sep 2006 09:26:49 -0700


On Sep 11, 2006, at 5:29 AM, email@hidden wrote:

I need to read in text files off a PC, manipulate some bytes of data and then
write the data back out to plain text files. How do I do this? It seems every
Cocoa application example I look at assumes that you want to use an encoder,
so all I can find are examples of reading and writing with encoders.


I just want to.

1: Read in the text file into an NSTextView
2: Have the user make some minor changes
3: Write the new data back out (To a Plain text file)

Is this possible??

The easiest way to do this is with NSAttributedString methods. An NSTextView has an NSTextStorage, which is a direct subclass of NSMutableAttributedString. You can use, for example, NSMutableAttributedString methods like - readFromURL:options:documentAttributes:error:. This method will, if you choose, read from any text format understood by the text system, not just plain text.


The most important wrinkle with plain text is that you should, wherever possible, use NSCharacterEncodingDocumentOption to specify an encoding for the file--otherwise in most cases the default encoding will be used, which is probably not what is wanted. You cannot in general expect to read a plain text file without knowing its encoding.

You can look at the TextEdit example code for some examples of this sort of thing in action.

Douglas Davidson

_______________________________________________
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


References: 
 >Reading/Writing Text files (From: email@hidden)

  • Prev by Date: Unit testing core data
  • Next by Date: Re: Re: clicking through
  • Previous by thread: Re: Reading/Writing Text files
  • Next by thread: Re: Reading/Writing Text files
  • Index(es):
    • Date
    • Thread