Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
June 18, 2012 Formatted input from text file | ||||
---|---|---|---|---|
| ||||
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); } |
June 18, 2012 Re: Formatted input from text file | ||||
---|---|---|---|---|
| ||||
Posted in reply to IK | 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 |
June 18, 2012 Re: Formatted input from text file | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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); } |
June 18, 2012 Re: Formatted input from text file | ||||
---|---|---|---|---|
| ||||
Posted in reply to IK | 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; } |
June 19, 2012 Re: Formatted input from text file | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | 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 ---------------- |
June 19, 2012 Re: Formatted input from text file | ||||
---|---|---|---|---|
| ||||
Posted in reply to IK | 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? |
Copyright © 1999-2021 by the D Language Foundation