I have run into some unexpected behavior by io:fread.<br /><br />I was wondering if someone could check whether there is something wrong with the way I use io:fread or whether there is a bug in io:fread.<br /><br />I have a text file which contains a "triangle of numbers"as follows:<br /><br /><pre><br />59<br />73 41<br />52 40 09<br />26 53 06 34<br />10 51 87 86 81<br />61 95 66 57 25 68<br />90 81 80 38 92 67 73<br />30 28 51 76 81 18 75 44<br />...<br /></pre><br /><br />There is a single space between each pair of numbers and each line ends with a carriage-return new-line pair.<br /><br />I use the following Erlang program to read this file into a list.<br /><br /><pre><br />-module(euler67).<br />-author('Cayle Spandon').<br /><br />-export([solve/0]).<br /><br />solve() -><br /> {ok, File} = file:open("triangle.txt", [read]),<br /> Data = read_file(File),<br /> ok = file:close(File),<br /> Data.<br /><br />read_file(File) -><br /> read_file(File, []).<br /><br />read_file(File, Data) -><br /> case io:fread(File, "", "~d") of<br /> {ok, [N]} -> <br /> read_file(File, [N | Data]);<br /> eof -><br /> lists:reverse(Data)<br /> end.<br /></pre><br /><br />The output of this program is:<br /><br /><pre><br />(erlide@cayle-spandons-computer.local)30> euler67:solve().<br />[59,73,41,52,40,9,26,53,6,3410,51,87,86,8161,95,66,57,25,<br /> 6890,81,80,38,92,67,7330,28,51,76,81|...]<br /></pre><br /><br />Note how the last number of the fourth line (34) and the first number of the fifth line (10) have been merged into a single number 3410.<br /><br />When I dump the text file using "od" there is nothing special about those lines; they end with cr-nl just like any other line:<br /><br /><pre><br />> od -t a triangle.txt<br />0000000 5 9 cr nl 7 3 sp 4 1 cr nl 5 2 sp 4 0<br />0000020 sp 0 9 cr nl 2 6 sp 5 3 sp 0 6 sp 3 4<br />0000040 cr nl 1 0 sp 5 1 sp 8 7 sp 8 6 sp 8 1<br />0000060 cr nl 6 1 sp 9 5 sp 6 6 sp 5 7 sp 2 5<br />0000100 sp 6 8 cr nl 9 0 sp 8 1 sp 8 0 sp 3 8<br />0000120 sp 9 2 sp 6 7 sp 7 3 cr nl 3 0 sp 2 8<br />0000140 sp 5 1 sp 7 6 sp 8 1 sp 1 8 sp 7 5 sp<br />0000160 4 4 cr nl 8 4 sp 1 4 sp 9 5 sp 8 7 sp<br /></pre><br /><br />One interesting observation is that some of the numbers for which the problem occurs happen to be on 16-byte boundary in the text file (but not all, for example 6890).