Created
September 24, 2019 12:27
-
-
Save tonetheman/b34da7d2128cbeaeddabb87de7e35417 to your computer and use it in GitHub Desktop.
read a file with nim line by line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc readwholefile() = | |
let data = readFile("data.txt") | |
echo(data) | |
proc justoneline() = | |
let f = open("data.txt") | |
defer: f.close() | |
let firstline = f.readLine() | |
echo(firstline) | |
proc testreads() = | |
let f = open("data.txt") | |
defer: | |
f.close() | |
var ll = f.read_line() | |
echo(ll) | |
ll = f.read_line() | |
echo(ll) | |
ll = f.read_line() | |
echo(ll) | |
ll = f.read_line() | |
echo(ll) | |
proc testfor() = | |
let f = open("data.txt") | |
defer: f.close() | |
var line : string | |
while f.read_line(line): | |
echo(line) | |
proc testcustom_iter1() = | |
type | |
CustomFile = object | |
name : string | |
inf : File | |
iterator items(range: CustomFile): string = | |
var line : string | |
while range.inf.read_line(line): | |
yield line | |
var inf : CustomFile | |
inf = CustomFile(name : "data.txt", | |
inf : open("data.txt")) | |
for s in items(inf): | |
echo(s) | |
proc test_iter2() = | |
iterator fitems(name : string) : string = | |
let f = open(name) | |
defer: f.close() | |
var line : string | |
while f.read_line(line): | |
yield line | |
for s in fitems("data.txt"): | |
echo("s",s) | |
proc test_iter3 = | |
iterator `$`(filename : string) : string = | |
let f = open(filename) | |
defer: | |
f.close() | |
var line : string | |
while f.read_line(line): | |
yield line | |
for line in $("data.txt"): | |
echo(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment