Vely logo install | documentation | examples | articles | changelog
16.6.0 released on Mar 08, 2023 | articles updated on Mar 20, 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)


Copyright (c) 2017-2023 DaSoftver LLC. Vely is a trademark of Dasoftver LLC. The software and information herein are provided "AS IS" and without any warranties or guarantees of any kind. Vely elephant logo (c) 2022 DaSoftver LLC. This web page is licensed under CC-BY-SA-4.0. Contact email vely@vely.dev.