18.4.0 released Sep 25, 2023
Read line

Purpose: Read text file line by line in a loop.

read-line <file> to [ define ] <line content> [ status [ define ] <length/status> ] [ delimiter <delimiter> ]

<any code>

end-read-line

read-line starts the loop in which a text <file> is read line by line into <line content> (which can be created with "define" if it doesn't exist), with end-read-line ending this loop. Once the end of file has been reached, or an error occurs, the loop exits.

The length of a text line can be obtained with optional <length/status>, which will be VV_ERR_READ if there is an error in reading file, or VV_ERR_OPEN if file cannot be opened, or VV_OKAY if end-of-file has been reached.

Check <length/status> variable within the loop to obtain the length of the currently read-in line, or after the loop for successful completion or an error condition.

Buffer allocated for each line read cannot be used in other iterations (i.e. when other lines are read) or outside the code between read-line and end-read-line. For that reason, if you want to save a read-in line for use elsewhere, save a copy of it using copy-string.

<delimiter> separates the lines in the file, and is by default new line, however it can be any character (note that it is not a string, but a single character).

Each line is null-terminated and new line (or a <delimiter>) remains if it was present (last line may not have it). There is no limit on line length, and Vely will automatically adjust the size of <line content> to accommodate the line length.

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

Use standard C break and continue statements to exit and continue the loop.
Examples
To read a text file line by line, and display as a web page with line breaks:
read-line "/home/user/dir/file" to define one_line status define len
    @Line length is <<p-num len>>, line is <<p-web one_line>><br/>
end-read-line

To read a text file delimited by "|" character:
read-line "/home/user/dir/file" to define one_line status define len delimiter '|'
...

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


You are free to copy, redistribute and adapt this web page (even commercially), as long as you give credit and provide a link back to this page (dofollow) - see full license at CC-BY-4.0. Copyright (c) 2019-2023 Dasoftver LLC. Vely and elephant logo are trademarks of Dasoftver LLC. The software and information on this web site are provided "AS IS" and without any warranties or guarantees of any kind.