Read & write txt files
Read & write txt files
I read the help file but I am not sure how I can create a txt file, write and then read a txt file.
- Morten|Dyalog
- Posts: 460
- Joined: Tue Sep 09, 2008 3:52 pm
Re: Read & write txt files
Hi Larry,
The easiest way to write text to a file is to use ⎕NPUT:
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:
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.
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.
Re: Read & write txt files
Thanks for the help. There is a program in linux that I would like to use. Is there an APL command I could use which will use the linux program and the txt file I made with ⎕NPUT?
- Morten|Dyalog
- Posts: 460
- Joined: Tue Sep 09, 2008 3:52 pm
Re: Read & write txt files
You can call a Linux program using the system function ⎕SH with a Linux command as the right argument. Any output from the command should be returned as the result:
If your Linux program creates a file, you should be able to use ⎕NGET to retrieve the contents as described earlier.
If you need to provide the file as input to your Linux program, the way to do that will depend on how that program is constructed. Can you provide any further detail?
Code: Select all
⎕SH'ls -l /home/brian'
total 152
-rw-r--r-- 1 root root 89140 Dec 6 16:34 default.dlf
drwxr-xr-x 2 brian brian 4096 Nov 30 04:59 Desktop
drwxrwxr-x 16 brian brian 4096 Dec 6 13:41 DFS
...etc...
If your Linux program creates a file, you should be able to use ⎕NGET to retrieve the contents as described earlier.
If you need to provide the file as input to your Linux program, the way to do that will depend on how that program is constructed. Can you provide any further detail?
- AndyS|Dyalog
- Posts: 263
- Joined: Tue May 12, 2009 6:06 pm
Re: Read & write txt files
It's worth taking a look at Calling UNIX Commands in the Dyalog for UNIX UI Guide which describes the use of ⎕SH more fully .. in particular you should note how to deal with redirecting stderr, and in coping with commands that return non-zero exit codes.
Note also that ⎕NGET doesn't work with files which claim to have a size of 0 bytes, but in fact "contain" data; this includes files such as pipes, FIFOs and many of the files in /proc. For these you will have to fall back to using ⎕NTIE, ⎕NREAD, ⎕NUNTIE.
Note also that ⎕NGET doesn't work with files which claim to have a size of 0 bytes, but in fact "contain" data; this includes files such as pipes, FIFOs and many of the files in /proc. For these you will have to fall back to using ⎕NTIE, ⎕NREAD, ⎕NUNTIE.
Re: Read & write txt files
This is my goal. I would use APL to create a file in the abc language which is
basically a character file and then use a linux program which I downloaded. The program converts it to a midi file I would then use VLC media player or Banshee to
listen to the midi file.
basically a character file and then use a linux program which I downloaded. The program converts it to a midi file I would then use VLC media player or Banshee to
listen to the midi file.
Re: Read & write txt files
Morten|Dyalog wrote:You can call a Linux program using the system function ⎕SH with a Linux command as the right argument. Any output from the command should be returned as the result:Code: Select all
⎕SH'ls -l /home/brian'
total 152
-rw-r--r-- 1 root root 89140 Dec 6 16:34 default.dlf
drwxr-xr-x 2 brian brian 4096 Nov 30 04:59 Desktop
drwxrwxr-x 16 brian brian 4096 Dec 6 13:41 DFS
...etc...
If your Linux program creates a file, you should be able to use ⎕NGET to retrieve the contents as described earlier.
If you need to provide the file as input to your Linux program, the way to do that will depend on how that program is constructed. Can you provide any further detail?
In your example, is it possible to create a variable in Dyalog containing /home/brian and then using ⎕SH to use Linux?
Example: ⎕SH'ls -l variable'
- Morten|Dyalog
- Posts: 460
- Joined: Tue Sep 09, 2008 3:52 pm
Re: Read & write txt files
larry316 wrote:is it possible to create a variable in Dyalog containing /home/brian and then using ⎕SH to use Linux?
Example: ⎕SH'ls -l variable'
You could write that as:
Code: Select all
⎕SH 'ls -l ''',variable,''''
The above creates an array of characters where the contents of (variable) have been surrounded by single quotes, and that array is passed a the argument to the ⎕SH system function..
Re: Read & write txt files
Morten|Dyalog wrote:larry316 wrote:is it possible to create a variable in Dyalog containing /home/brian and then using ⎕SH to use Linux?
Example: ⎕SH'ls -l variable'
You could write that as:Code: Select all
⎕SH 'ls -l ''',variable,''''
The above creates an array of characters where the contents of (variable) have been surrounded by single quotes, and that array is passed a the argument to the ⎕SH system function..
Thank you very much,that's exactly what I wanted.
Re: Read & write txt files
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.