Thread overview | ||||||
---|---|---|---|---|---|---|
|
July 19, 2009 Error: conversion, reading from file | ||||
---|---|---|---|---|
| ||||
I've having some trouble reading from a file. I am trying to read a list of high scores, each on a separate line, like this: 440 0 0 0 0 But with my code, I get an error saying: Error: conversion. Code: //Load high scores char[] file; int[] highScoreList; int nextScore; file = cast(char[])read( "highscore.txt" ); for( int i = 0; i < 5; i++ ) { nextScore = std.regexp.find( file, "\n" ); highScoreList ~= toInt( file[ 0 .. nextScore ] ); writefln(highScoreList[i]); file = file[ nextScore .. $ ]; } The output I get is: 440 Error: conversion The high scores that are printed above is what is in the file. I am using DMD1.046 with Phobos. |
July 19, 2009 Re: Error: conversion, reading from file | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michael P. | Michael P.:
> The output I get is:
> 440
> Error: conversion
>
> The high scores that are printed above is what is in the file. I am using DMD1.046 with Phobos.
Try:
import std.string: strip;
...
highScoreList ~= toInt(strip(file[0 .. nextScore]));
I have never understand the rationale of not ignoring whitespace inside the toInt, I think it's an ugly design.
Bye,
bearophile
|
July 19, 2009 Re: Error: conversion, reading from file | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | bearophile Wrote:
.
>
> Try:
> import std.string: strip;
> ...
> highScoreList ~= toInt(strip(file[0 .. nextScore]));
>
> I have never understand the rationale of not ignoring whitespace inside the toInt, I think it's an ugly design.
>
> Bye,
> bearophile
That didn't work for me. Doing this:
nextScore = std.regexp.find( file, "\n" );
highScoreList ~= toInt( file[ 0 .. nextScore ] );
file = file[ nextScore + 1 .. $ ]; //changed from: file = file[ nextScore .. $ ];
Worked though.
|
July 20, 2009 Re: Error: conversion, reading from file | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile |
>
> I have never understand the rationale of not ignoring whitespace inside the toInt, I think it's an ugly design.
>
Agreed!
|
Copyright © 1999-2021 by the D Language Foundation