GOLF(2gg)

πŸ“¦ version 601.4.41, in ubuntu questing

πŸ’– GOLF(2gg) β€” the read-line glow-up πŸ’–

okay bestie, we're in the 2gg section of the manual, aka the GOLF docs, and today's episode is all about read-line. pull up a chair, grab a strawberry πŸ“, and let's get into it.


✨ NAME ✨

read-line β€” reads stuff from files, one line at a time

🎯 PURPOSE 🎯

Read a text file line by line, in a loop. that's the whole vibe. no cap. πŸ“–

🧚 SYNTAX 🧚

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

<any code>

end-read-line

like, you open the loop with read-line, you put whatever code you want in the middle, and you close it with end-read-line. iconic structure, honestly. πŸ‘‘

πŸ’… DESCRIPTION πŸ’…

so read-line is what starts the loop, and inside that loop a text <file> gets read line by line into the string <line content>. end-read-line is the closer β€” it ends the loop. once you've hit the end of <file>, or an error pops off, the loop dips out. bye bestie πŸ‘‹

<file> can be a full path name, or a path relative to the application home directory (check the directories page for that one, iykyk).

now the <status> number β€” pay attention, this is important fr fr:

  • it's GG_ERR_READ if something goes wrong reading the file
  • it's GG_ERR_OPEN if the file literally cannot be opened
  • it's GG_OKAY if end-of-file has been reached

check for errors after the end-read-line statement, not before, okay? and if a line was read successfully? then <status> is its length. slay. πŸ“

<line content> gets allocated when a line is read, and it gets freed just before the next line is read β€” or if there are no more lines left to read at all. so if you wanna use <line content> outside of this scope, you gotta save it or stash it somewhere first. it will NOT wait for you. πŸ’€

the string <delimiter> is what separates the lines in the file, and by default it's a new line β€” but honestly it can be any character (just know it's the first character of the string <delimiter> that counts).

and a new line (or a <delimiter>) stays in <line content> if it was present in the file. note that the very last line may not have it. πŸ™ˆ

want to bounce out of the loop early or skip ahead? use the break-loop and continue-loop statements. main character energy, honestly.

🌈 EXAMPLES 🌈

exhibit a: reading a text file line by line, then displaying it as a web page with line breaks:

read-line "/home/bear/tmp/ll/filexx" to one_line status st
    string-length one_line to line_len
    @Line length is <<print-out line_len>>, line is <<p-web one_line>> status <<print-out st>><br/>
end-read-line
if-true st lesser-than 0
    get-req error to err
    @Error in reading, error [<<print-out err>>]
end-if

exhibit b: reading a text file that's delimited by the "|" character:

read-line "/home/user/dir/file" to one_line status len delimiter '|'
...

πŸ‘€ SEE ALSO πŸ‘€

Files

change-mode 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


quick reminder bestie: this page is from $VERSION, dated $DATE, and yes, it still lives under the GOLF(2gg) banner. that's the whole page. go read some files. mwah πŸ’–
πŸ“¦ where this page comes from