Open file

Purpose: Open file for reading and writing.

 open-file <file name> file-id <file id> \
     [ new-truncate ] \
     [ status <status> ]

Opens file given by <file name> for reading and writing and creates an open file variable identified by <file id>.

<file name> can be a full path name, or a path relative to the application home directory (see directories).

You can obtain the status of file opening via <status> number (in "status" clause). The <status> is GG_OKAY if file is opened, or GG_ERR_OPEN if could not open file.

If "new-truncate" clause is used, a new file is created if it doesn't exist, or it is truncated if it does.
Examples
Create a file (or truncate an existing one), write 25,000 rows and the read back those rows and display them, then close file:
 %% /ofile
     // Create new file, or truncate an old file if it exists
     open-file "testwrite" file-id nf new-truncate

     // Write 25000 rows
     start-loop repeat 25000 use i
        (( line
        @some text in line <<p-num i>>
        )) notrim
        string-length line to line_len
        write-file file-id nf from line length line_len
     end-loop

     // Rewind back to the beginning
     file-position set 0 file-id nf

     // Read all 25000 rows back, and print them out
     start-loop repeat 25000 use i
        read-file file-id nf  to one_item
        p-out one_item
     end-loop

     // Close the file
     close-file file-id nf
 %%

See also
Files
close-file  
copy-file  
delete-file  
file-position  
file-storage  
file-uploading  
lock-file  
open-file  
read-file  
read-line  
rename-file  
stat-file  
temporary-file  
uniq-file  
unlock-file  
write-file  
See all
documentation


Copyright (c) 2019-2024 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.