GOLF(2gg)
π read-file π
okay bestie, welcome to the GOLF(2gg) manual, Development section, and today's main character is read-file π she's a files girlie, she reads, she slays. let's get into it.
β¨ NAME β¨
read-file - (files)
π¦ PURPOSE π¦
Read file into a string variable. that's the whole job. no cap, that's it, and she does it gorgeously π
π SYNTAX π
read-file <file> | ( file-id <file id> ) \
to <content> \
[ position <position> ] \
[ length <length> ] \
[ status <status> ] \
[ end-of-file <eof> ]
like, look at her. optional clauses in brackets, backslash line continuations, the whole fit is giving structured π«Ά
π DESCRIPTION π
πΈ WITHOUT FILE-ID πΈ
this is the simple method of reading a file, the low-effort-but-effective queen. the file named file gets opened, the data gets read, and then the file gets closed. in, out, no drama. periodt.
file can be a full path name, or a path relative to the application home directory (see directories). so either you spell out the whole thing or you keep it cozy relative to home, your call bestie.
the data that gets read is stored into the string content. and note β the file can be binary or text, and content may have null-bytes. so don't come at her acting surprised when there's a \0 in there, she warned you π
- if the
positionandlengthclauses are not specified,read-filereads the entire file into content. the whole thing. every byte. she's thorough π - if the
positionclause is used, then reading starts at byte position. otherwise it starts at the beginning of the file. and a position of zero (0) represents the beginning of the file. zero-indexed girlies rise up β¨ - if the
lengthclause is used, then length number of bytes is read. otherwise the rest of the file is read. and if length is0, then content is an empty string and status is0. empty girl, empty status, no problem π - if the
statusclause is used, then the number of bytes read is stored to status β unless an error occurred, in which case status is negative and holds the error code. and the error code can be:GG_ERR_POSITIONβ if position is negative, outside the file, or the file does not support it πGG_ERR_READβ if length is negative or there's an error reading the file πGG_ERR_OPENβ if the file cannot be opened π«
- if the number of bytes read isn't what's requested, you can use the
end-of-fileclause to get the boolean eof, which istrueif the end of file happened, orfalseif there is an error β and in that error case you can use theerrnoclause inget-reqto obtain the actual reason for the error. iykyk, always check your errno bestie π«Ά
π§ WITH FILE-ID π§
this method uses a file id that was created with open-file. she's the commitment version, the main character energy one π
you can then read (and write) the file using this file id, and the file stays open until close-file is called, or until the request ends β i.e. Golf will automatically close any such open files for you. so like, she's got your back, no leaks π
the data that gets read is stored into the string content. and again β file can be binary or text, and content may have null-bytes. same deal, same warning, same vibe.
- if the
positionclause is used, then data is read starting from byte position (with position of0being the first byte). otherwise reading starts from the current file position, which is determined by the previous reads/writes, or as set by using thesetclause infile-position. and note: after each read or write, the file position is advanced by the number of bytes read or written. she keeps track, she's organized like that β¨ - if the
lengthclause is used, then length number of bytes is read. otherwise the rest of the file is read. and if length is0, then content is an empty string and status is0. same as above, consistent queen πΈ - note that when you reach the end of file and no more bytes can be read, status is
0. yes,0. not an error. she's just done, respectfully π - if the
statusclause is used, then the number of bytes read is stored to status β unless an error occurred, in which case status holds the error code. and the error code can be:GG_ERR_POSITIONβ if position is negative, outside the file, or the file does not support it πGG_ERR_READβ if length is negative or there's an error reading the file πGG_ERR_OPENβ if the file is not open π«
- if the number of bytes read isn't what's requested, you can use the
end-of-fileclause to get the boolean eof, which istrueif the end of file happened, orfalseif there is an error β and in that error case you can use theerrnoclause inget-reqto obtain the actual reason for the error. fr fr, the errno clause is your bestie in a crisis π
π₯ EXAMPLES π₯
scenario one: read the entire file and create both the variable that holds its content and the status variable. chef's kiss setup π
read-file "/home/user/some_file" to file_content status st
if-true st greater-than 0
@Read:
@<hr/>
p-web file_content
@<hr/>
else-if
@Could not read (<<print-format "%ld", st>>)
end-if
look at that if-true doing a vibe check on st greater-than 0, and the else-if catching the L with <<print-format "%ld", st>>. iconic π
scenario two: read 10 bytes starting at position 20 (with position 0 being the first byte). precision reading, we love to see it β¨
read-file "/home/user/some_file" to file_content position 20 length 10
and if you want the β¨file-idβ¨ flavor, go see open-file for an example with the file-id clause. she's linked below, go say hi π«Ά
π SEE ALSO π
Fileschange-modeclose-filecopy-filedelete-filefile-positionfile-storagefile-uploadinglock-fileopen-fileread-fileread-linerename-filestat-filetemporary-fileuniq-fileunlock-filewrite-file- See all documentation π
okay that's the whole page bestie, you're officially a read-file expert, go read some files, go be the main character, go off π this one was stamped with $DATE and version $VERSION, filed under GOLF(2gg) β mwah, bye! πβ¨
π¦ where this page comes from
- ubuntu questing: golf 601.4.41-1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms