std.file.readText() extra Line Feed character

yazd via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Dec 18 02:43:31 PST 2014


On Thursday, 18 December 2014 at 10:16:38 UTC, Colin wrote:
> On Thursday, 18 December 2014 at 09:25:47 UTC, ketmar via 
> Digitalmars-d-learn wrote:
>> On Thu, 18 Dec 2014 09:18:35 +0000
>> Colin via Digitalmars-d-learn 
>> <digitalmars-d-learn at puremagic.com> wrote:
>>
>>> Why does std.file.readText() append a Line Feed char onto the 
>>> end of the string?
>>> 
>>> I have a file with the following contents in it:
>>> Name   =               Int
>>> Other=Float
>>> One More = String(Random;)
>>> 
>>> I then have the code:
>>> 
>>> void main(string[] args){
>>>     const text = "Name   =               Int
>>> Other=Float
>>> One More = String(Random;)";
>>> 
>>>     string input = readText(args[1]);
>>> 
>>>     writefln("Raw data");
>>>     writefln("D)    %s", cast(ubyte[])text[$-5..$]);
>>>     writefln("File) %s", cast(ubyte[])input[$-5..$]);
>>> 
>>> }
>>> 
>>> This produces:
>>> Raw data
>>> D)    [100, 111, 109, 59, 41]
>>> File) [111, 109, 59, 41, 10]
>>> 
>>> Any Idea why the reading from the File adds on that extra 
>>> '10' character?
>>> 
>>> I don't think it's my editor adding chars to the end of the 
>>> file, as I'm using vi.
>>
>> you *definetely* has the last line ended with '\n'.
>
> I dont see how, I copy and pasted from the string definition in 
> D, directly after the first " and directly before the last ".
>
> If I look at the file in vim with line numbers turned on, the 
> file is like this. So I really dont think I have a new line in 
> the file...
>
>   1 Name   =               Int
>   2 Other=Float
>   3 One More = String(Random;)

You can make sure using `hexdump -C file`. I tested locally 
creating a file using vi, and it does indeed have a '\n' at the 
end of file.


More information about the Digitalmars-d-learn mailing list