Re: Range violation error when reading from a file

2019-06-20 Thread Samir via Digitalmars-d-learn

On Tuesday, 18 June 2019 at 09:42:41 UTC, aliak wrote:

On Tuesday, 18 June 2019 at 01:15:54 UTC, Samir wrote:

On Monday, 17 June 2019 at 03:46:11 UTC, Norm wrote:
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.


I mean this:

$ dmd -run readfile.d
1)
file.eof() == false
line = "> line 1"
writeln("lines 1" + \n);
2)
file.eof() == false
line = line 2
writeln("line 2" + \n);

...snip...

6)
file.eof() == false
line = "" // empty since there're no lines left in file
writeln("" + \n); <-- this is your blank line
7)
file.eof() == true


Got it!  Now I see what you were saying.  Thanks for taking the 
time to provide a detailed explanation!


Re: Range violation error when reading from a file

2019-06-18 Thread aliak via Digitalmars-d-learn

On Tuesday, 18 June 2019 at 01:15:54 UTC, Samir wrote:

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.


I mean this:

$ dmd -run readfile.d
1)
file.eof() == false
line = "> line 1"
writeln("lines 1" + \n);
2)
file.eof() == false
line = line 2
writeln("line 2" + \n);
3)
file.eof() == false
line = line 3
writeln("line 3" + \n);
4)
file.eof() == false
line = > line 4
writeln("> line 4" + \n);
5)
file.eof() == false
line = line 5
writeln("line 5" + \n);
6)
file.eof() == false
line = "" // empty since there're no lines left in file
writeln("" + \n); <-- this is your blank line
7)
file.eof() == true


Re: Range violation error when reading from a file

2019-06-17 Thread Samir via Digitalmars-d-learn

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!


Re: Range violation error when reading from a file

2019-06-17 Thread aliak via Digitalmars-d-learn

On Monday, 17 June 2019 at 00:22:23 UTC, Samir wrote:


Also, if I run the program below with the same file, I don't 
get any range violation errors:


Ya, writeln will not access individual elements of a range if 
there aren't any. So no violations occur.


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.




Any suggestions on how to rectify?



You can do:

if (!line.length) {
continue;
}

Inside your while loop after the call to strip.



Re: Range violation error when reading from a file

2019-06-16 Thread Norm via Digitalmars-d-learn

On Monday, 17 June 2019 at 00:22:23 UTC, Samir wrote:

On Sunday, 16 June 2019 at 23:55:41 UTC, lithium iodate wrote:
There is *very* likely to be a terminating new-line at the end 
of the file (many editors add one without asking!). If that 
the case, the last line seen by the loop will be empty and you 
must not attempt to access any elements.


On Monday, 17 June 2019 at 00:02:37 UTC, aliak wrote:
The fail bit is only set after reading fails. So after you 
read the last line, your eof will still return true, and hence 
your range violation.


H...maybe you and lithium iodate were onto something.

Here is what the file looks like in vim:
> line 1
line 2
line 3
> line 4
line 5
~
~
~

The "5" in the last line is the last character I can put my 
cursor on.


Also, if I run the program below with the same file, I don't 
get any range violation errors:


import std.stdio;
import std.string;

void main() {
File file = File("test.txt");
string line;

while (!file.eof()) {
line = file.readln().strip;
//if (line[0] == '>') { // line 10
//writeln(line[1..$]);
//}
//else {
writeln(line);
//}
}
}

HOWEVER, the output is interesting.  There IS a blank line 
between the last line and the prompt:


$ dmd -run readfile.d
> line 1
line 2
line 3
> line 4
line 5

$

Any suggestions on how to rectify?


You could change the IF to

`if(line.length > 0 && line[0] == '>')`

or use strip itself;

`File("test.txt", "r").byLine.map!(line => 
line.strip(">")).writeln;`


For "> line 1" your code and this above will generate " line 1" 
(note the leading space). To remove that change the string passed 
to `strip` to include a space, e.g.;


`.strip("> ")).writeln;`

bye,
Norm



Re: Range violation error when reading from a file

2019-06-16 Thread Samir via Digitalmars-d-learn

On Sunday, 16 June 2019 at 23:55:41 UTC, lithium iodate wrote:
There is *very* likely to be a terminating new-line at the end 
of the file (many editors add one without asking!). If that the 
case, the last line seen by the loop will be empty and you must 
not attempt to access any elements.


On Monday, 17 June 2019 at 00:02:37 UTC, aliak wrote:
The fail bit is only set after reading fails. So after you read 
the last line, your eof will still return true, and hence your 
range violation.


H...maybe you and lithium iodate were onto something.

Here is what the file looks like in vim:
> line 1
line 2
line 3
> line 4
line 5
~
~
~

The "5" in the last line is the last character I can put my 
cursor on.


Also, if I run the program below with the same file, I don't get 
any range violation errors:


import std.stdio;
import std.string;

void main() {
File file = File("test.txt");
string line;

while (!file.eof()) {
line = file.readln().strip;
//if (line[0] == '>') { // line 10
//writeln(line[1..$]);
//}
//else {
writeln(line);
//}
}
}

HOWEVER, the output is interesting.  There IS a blank line 
between the last line and the prompt:


$ dmd -run readfile.d
> line 1
line 2
line 3
> line 4
line 5

$

Any suggestions on how to rectify?



Re: Range violation error when reading from a file

2019-06-16 Thread aliak via Digitalmars-d-learn

On Sunday, 16 June 2019 at 23:44:49 UTC, Samir wrote:

On Sunday, 16 June 2019 at 23:03:04 UTC, aliak wrote:

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


The last line of the file is just text but without a newline 
(\n) character or any other whitespace character at the end.  I 
get the same error when I remove the strip function from the 
readln line.


http://www.cplusplus.com/reference/ios/ios/eof/

The fail bit is only set after reading fails. So after you read 
the last line, your eof will still return true, and hence your 
range violation.


Re: Range violation error when reading from a file

2019-06-16 Thread lithium iodate via Digitalmars-d-learn

On Sunday, 16 June 2019 at 23:44:49 UTC, Samir wrote:

On Sunday, 16 June 2019 at 23:03:04 UTC, aliak wrote:

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


The last line of the file is just text but without a newline 
(\n) character or any other whitespace character at the end.  I 
get the same error when I remove the strip function from the 
readln line.


There is *very* likely to be a terminating new-line at the end of 
the file (many editors add one without asking!). If that the 
case, the last line seen by the loop will be empty and you must 
not attempt to access any elements.


Re: Range violation error when reading from a file

2019-06-16 Thread Samir via Digitalmars-d-learn

On Sunday, 16 June 2019 at 23:03:04 UTC, aliak wrote:

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


The last line of the file is just text but without a newline (\n) 
character or any other whitespace character at the end.  I get 
the same error when I remove the strip function from the readln 
line.


Re: Range violation error when reading from a file

2019-06-16 Thread Samir via Digitalmars-d-learn

On Sunday, 16 June 2019 at 23:03:04 UTC, aliak wrote:
stripping the last line could result in an empty line if it 
just has strippable characters?


The last line is just the text of the last line.  There is no 
newline character at the end.  I also get the same error if I 
remove the strip function from the readln line.


Re: Range violation error when reading from a file

2019-06-16 Thread aliak via Digitalmars-d-learn

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@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?


Range violation error when reading from a file

2019-06-16 Thread Samir via Digitalmars-d-learn

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@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