How do you read a file line by line? #14840
Replies: 6 comments 6 replies
-
You don't. Not as a simple library call, at least. You can read all the lines from a file into an array of strings, one array entry per line of the file. You can read all the bytes of a file into a byte array, and step through it yourself to find EOL characters. You can use a buffered reader to read bytes from a file until you hit an EOL, then save that as a string. Etc. |
Beta Was this translation helpful? Give feedback.
-
File streams are planned. They are necessary for very large files. |
Beta Was this translation helpful? Give feedback.
-
Possibly the OP is referring to
For the sake of the OP... A simple example of possible usage would kind of be like below.
|
Beta Was this translation helpful? Give feedback.
-
Just in case anyone finds this, this has been implemented. import os
fn main() {
file_path := 'path/to/your/file.txt'
mut file := os.open(file_path) or {
eprintln('Failed to open the file: $err')
return
}
defer { file.close() } // Automatically close the file when the function returns
// Read lines from the file
for {
line := file.read_line() or { break }
println(line)
}
}
|
Beta Was this translation helpful? Give feedback.
-
Well noted.Thanks for the reply!
…________________________________
发件人: Henrik Holst ***@***.***>
发送时间: 2023年12月28日 8:42
收件人: vlang/v ***@***.***>
抄送: LukyGuyLucky ***@***.***>; Comment ***@***.***>
主题: Re: [vlang/v] How do you read a file line by line? (Discussion #14840)
Sorry! I did not test the code. It turns out that the API I thought I saw was not on File but on BufferedReader:
https://github.com/vlang/v/blob/master/examples/buf_reader.v
―
Reply to this email directly, view it on GitHub<#14840 (reply in thread)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/A2QC2FRM6HUGH5KIYXTO453YLS6ATAVCNFSM5ZVETYK2U5DIOJSWCZC7NNSXTOKENFZWG5LTONUW63SDN5WW2ZLOOQ5TOOJWGEYTSNQ>.
You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
vlang can directly call C code, so you can use C fgets() function in v:
|
Beta Was this translation helpful? Give feedback.
-
How do you read a file line by line?
Beta Was this translation helpful? Give feedback.
All reactions