Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when reading asynchronous text file line by line #10641

Open
mrhdias opened this issue Feb 11, 2019 · 6 comments
Open

Error when reading asynchronous text file line by line #10641

mrhdias opened this issue Feb 11, 2019 · 6 comments
Labels
Async Everything related to Nim's async

Comments

@mrhdias
Copy link
Contributor

mrhdias commented Feb 11, 2019

When I tried to read a text file asynchronous line by line it gives an error and only reads the first line.

Example

import asyncdispatch, asyncfile, os

proc main() {.async.} =
  var filename = "test.txt"
  var file = openAsync(filename, fmRead)
  while true:
    let line = await file.readLine()
    if line.len == 0: break
    echo line
  
  file.close()

waitFor main()

Current Output

Exception message: index out of bounds
Exception type: [IndexError]
Error: execution of an external program failed: '/home/hdias/Downloads/tmp/test '

Additional Information

$ nim -v
Nim Compiler Version 0.19.4 [Linux: amd64]
Compiled at 2019-02-01
Copyright (c) 2006-2018 by Andreas Rumpf

git hash: b6d96cafc8bcad1f3d32f2910b25cd11a93f7751
active boot switches: -d:release
@mratsim mratsim added the Async Everything related to Nim's async label Feb 12, 2019
@mrhdias
Copy link
Contributor Author

mrhdias commented Feb 12, 2019

With fix #10640 I get another error:

proc main() {.async.} =
    var fh = openAsync("test.txt", fmRead)
    while (let line = await fh.readLine(); line.len) > 0:
        echo line
    fh.close()

waitFor main()

$ nim c -r test.nim
...
Exception message: EOF reached
Exception type: [EOFError]
Error: execution of an external program failed: '/home/hdias/tmp/test '

@LemonBoy
Copy link
Contributor

I get another error

It's not an error, if you read past the EOF you get an EOFError the same way you'd get one by using the synchronous variant.

@mrhdias
Copy link
Contributor Author

mrhdias commented Feb 13, 2019

Ok, how to find the end of a file in this case?
The readLine() return a string. If not return nothing for me is the EOF.

proc main() {.async.} =
    var fh = openAsync("test.txt", fmRead)
    while (let line = await fh.readLine(); line.len) > 0:
        echo line
    fh.close()

waitFor main()

Example in C:

while((ch = fgetc(fp)) != EOF) {
    str[len++] = ch;
    if (ch=='\n')
        break;
}
str[len]= '\0';
return(str);

@LemonBoy
Copy link
Contributor

Ok, how to find the end of a file in this case?

proc main() {.async.} =
    var fh = openAsync("test.txt", fmRead)
    try:
        let line = await fh.readLine()
        echo line
    except EOFError:
        discard
    finally:
        fh.close()

waitFor main()

@mrhdias
Copy link
Contributor Author

mrhdias commented Feb 13, 2019

Ok, how to find the end of a file in this case?

proc main() {.async.} =
    var fh = openAsync("test.txt", fmRead)
    try:
        let line = await fh.readLine()
        echo line
    except EOFError:
        discard
    finally:
        fh.close()

waitFor main()

But with your code I read only one line!!!

@mrhdias
Copy link
Contributor Author

mrhdias commented Feb 13, 2019

Ok, how to find the end of a file in this case?

proc main() {.async.} =
    var fh = openAsync("test.txt", fmRead)
    try:
        let line = await fh.readLine()
        echo line
    except EOFError:
        discard
    finally:
        fh.close()

waitFor main()

But with your code I read only one line!!!

I find a way to make this work:

proc main() {.async.} =
    var fh = openAsync("test.txt", fmRead)
    while true:
        try:
            let line = await fh.readLine()
            echo line
        except EOFError:
            break
    fh.close()

waitFor main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Async Everything related to Nim's async
Projects
None yet
Development

No branches or pull requests

3 participants