Range violation error when reading from a file

aliak something at something.com
Sun Jun 16 23:03:04 UTC 2019


On Sunday, 16 June 2019 at 22:47:14 UTC, Samir wrote:
> I am trying to read from a text file using the following code:
>
> import std.stdio;
> import std.string;
>
> void main() {
>     File file = File("test.txt");
>     string line;
>
>     while (!file.eof()) {
>         line = strip(file.readln());
>         if (line[0] == '>') {         // line 10
>             writeln(line[1..$]);
>         }
>         else {
>             writeln(line);
>         }
>     }
> }
>
> and I get the following error AFTER the last line is processed:
>
> core.exception.RangeError at readfile.d(10): Range violation
> ----------------
> ??:? _d_arrayboundsp [0x448efa]
> ??:? _Dmain [0x4459f7]
>
> Any idea what I am doing wrong?  When processing the if 
> statement or writing the slice, am I inadvertently trying to 
> read a non-existent line in the file?
>
> Thanks in advance
> Samir

stripping the last line could result in an empty line if it just 
has strippable characters?


More information about the Digitalmars-d-learn mailing list