Read & write txt files

Learning APL or new to Dyalog? Ask "silly" questions here, without fear...

Re: Read & write txt files

Postby larry316 on Sun Jan 08, 2017 5:30 pm

larry316 wrote:
Morten|Dyalog wrote:Hi Larry,

The easiest way to write text to a file is to use ⎕NPUT:

Code: Select all
(⊂'This is some text',(⎕UCS 13 10),'2nd line')⎕NPUT'c:\temp\sometext.txt'

You can supply the text as a vector of vectors, or as a simple vector with embedded newlines (as above). Note that you need to enclose the text because you COULD specify the encoding to use as a 2nd element of the left argument.

⎕NGET allows us to read the text back, returning a 3-element vector which has a structure that would be suitable for use as the left argument to ⎕NPUT:

Code: Select all
      (text encoding newlines)←⎕NGET 'c:\temp\sometext.txt'
      text
This is some text                   
Here is a 2nd line                   

      ⍴text
37
      encoding
UTF-8-NOBOM
      newlines
13 10

Note that, although newlines tells us that ⎕NGET detected that the newline sequence found in the file was CRLF (13 10), the text has been "normalised" to have lines separated by a single line feed (10). This is done in order to help you write portable code, and to have text which is easier to process.



You mentioned create a vector of vectors. I was working on a function that created a numeric for the days of the week and use the numeric to output the date as characters. Example 1 would output Sunday,2 would output Monday etc.




Sorry forget my request. I guess I wasn't thinking too clearly
larry316
 
Posts: 16
Joined: Thu Dec 01, 2016 4:28 pm

Previous

Return to New to Dyalog?

Who is online

Users browsing this forum: No registered users and 1 guest