digitalmars.D.learn - Formatted input from text file
- IK (17/17) Jun 18 2012 I'm attempting Project Euler's problem 11.
- bearophile (17/18) Jun 18 2012 I don't know the cause of your problem. While investigating it I
- IK (12/12) Jun 18 2012 Hmm does your code generate a 1D `array` as a grid, bearophile?
- Timon Gehr (11/23) Jun 18 2012 IIRC formattedRead would require input similar to
- IK (31/31) Jun 19 2012 This program below fails if the file contains an empty line.
- IK (6/6) Jun 19 2012 Now I see! The error message is a bit awkward, it should show
- bearophile (4/6) Jun 19 2012 Hara knows. Maybe it's a bug.
I'm attempting Project Euler's problem 11.
http://projecteuler.net/problem=11
Currently, my code looks like posted below, and it gives
    object.Error: Access Violation
at runtime.
Question: how to read that grid from a text file? (See link above.)
If you have ideas for using something other than uint[20][20], please say.
Thank you.
import std.stdio;
import std.stream;
void main()
{
	uint[20][20] grid;
	BufferedFile input_file;
	input_file.open("p011_input.txt");
	input_file.readf("%(%(%d %)\n%)", grid);
}
 Jun 18 2012
IK:Question: how to read that grid from a text file?I don't know the cause of your problem. While investigating it I have filed another bug: http://d.puremagic.com/issues/show_bug.cgi?id=8260 In the meantime this (a bit scary) code solves your reading problem: import std.stdio, std.algorithm, std.range, std.conv; void main() { const grid = File("table.txt") .byLine().map!(r => std.algorithm.splitter(r) .map!(to!int)() .array())() .array(); writeln(grid); } Bye, bearophile
 Jun 18 2012
Hmm does your code generate a 1D `array` as a grid, bearophile?
Anyway thanks, I'll compile it shortly.
My own code evolved to what's below and gives a Range violation.
Also I don't understand why formattedRead can't just accept a casted
`opSlice.dup`, is it a bug?
void main()
{
	uint[20][20] grid;
	auto input_file = new MmFile("p011_input.txt");
	string temp = cast(string)input_file.opSlice;
	formattedRead(temp, "%(%(%s %)\n%)", &grid);
}
 Jun 18 2012
On 06/18/2012 05:11 PM, IK wrote:Hmm does your code generate a 1D `array` as a grid, bearophile? Anyway thanks, I'll compile it shortly. My own code evolved to what's below and gives a Range violation. Also I don't understand why formattedRead can't just accept a casted `opSlice.dup`, is it a bug?Yes.void main() { uint[20][20] grid; auto input_file = new MmFile("p011_input.txt"); string temp = cast(string)input_file.opSlice; formattedRead(temp, "%(%(%s %)\n%)",&grid); }IIRC formattedRead would require input similar to "[[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]]" This works: void main() { auto input_file = new MmFile("p011_input.txt"); auto text = cast(string)input_file[]; auto grid = text.splitLines.map!(_=>_.split.to!(uint[])).array; }
 Jun 18 2012
This program below fails if the file contains an empty line.
Why exactly does this happen?
Does ln get a mangled value when a line is empty?
     import std.format;
     import std.stdio;
     void main()
     {
         int[] test;
         foreach (ln; File("ints.txt").byLine)
             formattedRead(ln, "%(%d %)", &test);
         writefln("%(%d %)", test);
     }
std.conv.ConvException c:\dmd2\windows\bin\..\..\src\phobos\std\conv.d(1779): 
Unexpected '
' when converting from type char[] to type int
----------------
4169C4
41684F
405E63
405BDF
405A06
405964
40573F
4055CE
40246B
4020CC
4083A0
4083DA
407FFB
41DB21
----------------
 Jun 19 2012
Now I see! The error message is a bit awkward, it should show '\n' instead. (It should also use Windows line endings on a Windows machine, but that's a minor point.) So indeed formattedRead() fails for a newline. Can this be considered a bug?
 Jun 19 2012
IK:So indeed formattedRead() fails for a newline. Can this be considered a bug?Hara knows. Maybe it's a bug. Bye, bearophile
 Jun 19 2012








 
  
  
  "bearophile" <bearophileHUGS lycos.com>
 "bearophile" <bearophileHUGS lycos.com>