Thread overview |
---|
June 16, 2019 Range violation error when reading from a file | ||||
---|---|---|---|---|
| ||||
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 |
June 16, 2019 Re: Range violation error when reading from a file | ||||
---|---|---|---|---|
| ||||
Posted in reply to Samir | 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?
|
June 16, 2019 Re: Range violation error when reading from a file | ||||
---|---|---|---|---|
| ||||
Posted in reply to aliak | 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.
|
June 16, 2019 Re: Range violation error when reading from a file | ||||
---|---|---|---|---|
| ||||
Posted in reply to aliak | 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.
|
June 16, 2019 Re: Range violation error when reading from a file | ||||
---|---|---|---|---|
| ||||
Posted in reply to Samir | 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.
|
June 17, 2019 Re: Range violation error when reading from a file | ||||
---|---|---|---|---|
| ||||
Posted in reply to Samir | 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. |
June 17, 2019 Re: Range violation error when reading from a file | ||||
---|---|---|---|---|
| ||||
Posted in reply to aliak | 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. Hmmmm...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? |
June 17, 2019 Re: Range violation error when reading from a file | ||||
---|---|---|---|---|
| ||||
Posted in reply to Samir | 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.
>
> Hmmmm...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
|
June 17, 2019 Re: Range violation error when reading from a file | ||||
---|---|---|---|---|
| ||||
Posted in reply to Samir | 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. |
June 18, 2019 Re: Range violation error when reading from a file | ||||
---|---|---|---|---|
| ||||
Posted in reply to aliak | 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! |
Copyright © 1999-2021 by the D Language Foundation