Reading array of numbers from file

evilrat evilrat666 at gmail.com
Fri Jan 11 04:17:31 PST 2013


On Friday, 11 January 2013 at 09:44:31 UTC, Lubos Pintes wrote:
> Hi,
> Do I correctly suppose this is not possible? Because I don't 
> understand fully the compiler error.
>
> import std.stdio;
> void main() {
>   int[] a;
>   stdin.readf(" %s",&a);
>   writeln(a);
> }

what is the definition of format of "array" to read from file?

from your example it is reading formatted(%s = simply any string 
terminated by enter, also prepended by a space?) from standard 
input.

try something like this

import std.file, std.ascii;

void read()
{
// reads whole file to string, use std.file.byLine or others on 
large files
auto data = cast(string) read("path_to_file");

// array storing your data
int[] numbers;

// run through the all content readed from file
foreach(ch; data)
{
// if character is a number put it to numbers array
if ( std.ascii.isNumber(ch) )
   numbers ~= cast(int) ch;
}

}




More information about the Digitalmars-d-learn mailing list