Range violation error when reading from a file

Samir samir at aol.com
Tue Jun 18 01:15:54 UTC 2019


On Monday, 17 June 2019 at 03:46:11 UTC, Norm wrote:
> On Monday, 17 June 2019 at 00:22:23 UTC, Samir wrote:

>> Any suggestions on how to rectify?
>
> You could change the IF to
>
> `if(line.length > 0 && line[0] == '>')`

Thanks, Norm.  That seemed to do the trick and fixed the error.

On Monday, 17 June 2019 at 11:25:01 UTC, aliak wrote:
> On Monday, 17 June 2019 at 00:22:23 UTC, Samir wrote:

>> HOWEVER, the output is interesting.  There IS a blank line 
>> between the last line and the prompt:
>
> That's because you're using write*ln*. So even though line is 
> empty, you still output a new line.

Curious.  I am going to have to think about that for a bit as I 
don't quite understand.

>> Any suggestions on how to rectify?
>
>
> You can do:
>
> if (!line.length) {
>     continue;
> }
>
> Inside your while loop after the call to strip.

Thanks, aliak!  I think this is similar to Norm's suggestion in 
that I need to check for a non-zero line length before continuing.

What's funny now is that I get two blank lines after the output 
and before the prompt:

$ ./readfile
  line 1
line 2
line 3
  line 4
line 5


$

Ultimately, I think the original suggestions by you and lithium 
iodate about there being an empty line at the end is probably the 
culprit.  I will have to investigate that further.

Thank you to everyone that chimed in to help me out!


More information about the Digitalmars-d-learn mailing list