www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.file.readText() extra Line Feed character

reply "Colin" <grogan.colin gmail.com> writes:
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.
Dec 18 2014
next sibling parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Thu, 18 Dec 2014 09:18:35 +0000
Colin via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

 Why does std.file.readText() append a Line Feed char onto the end=20
 of the string?
=20
 I have a file with the following contents in it:
 Name   =3D               Int
 Other=3DFloat
 One More =3D String(Random;)
=20
 I then have the code:
=20
 void main(string[] args){
      const text =3D "Name   =3D               Int
 Other=3DFloat
 One More =3D String(Random;)";
=20
      string input =3D readText(args[1]);
=20
      writefln("Raw data");
      writefln("D)    %s", cast(ubyte[])text[$-5..$]);
      writefln("File) %s", cast(ubyte[])input[$-5..$]);
=20
 }
=20
 This produces:
 Raw data
 D)    [100, 111, 109, 59, 41]
 File) [111, 109, 59, 41, 10]
=20
 Any Idea why the reading from the File adds on that extra '10'=20
 character?
=20
 I don't think it's my editor adding chars to the end of the file,=20
 as I'm using vi.
you *definetely* has the last line ended with '\n'.
Dec 18 2014
parent reply "Colin" <grogan.colin gmail.com> writes:
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 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;)
Dec 18 2014
parent reply "yazd" <yazan.dabain gmail.com> writes:
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 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.
Dec 18 2014
parent reply "Colin" <grogan.colin gmail.com> writes:
On Thursday, 18 December 2014 at 10:43:32 UTC, yazd wrote:
 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 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.
Ah, I see. That's a little annoying. Thanks folks!
Dec 18 2014
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 12/18/2014 02:51 AM, Colin wrote:

 vi, and it does indeed have a '\n' at the end of file.
Ah, I see. That's a little annoying.
It looks like there are ways of dealing with it: http://stackoverflow.com/questions/1050640/vim-disable-automatic-newline-at-end-of-file Ali "happy with Emacs :p"
Dec 18 2014
parent reply "Colin" <grogan.colin gmail.com> writes:
On Thursday, 18 December 2014 at 22:29:30 UTC, Ali Çehreli wrote:
 On 12/18/2014 02:51 AM, Colin wrote:

 vi, and it does indeed have a '\n' at the end of file.
Ah, I see. That's a little annoying.
It looks like there are ways of dealing with it: http://stackoverflow.com/questions/1050640/vim-disable-automatic-newline-at-end-of-file Ali "happy with Emacs :p"
Does emacs do this aswell? :) Anyway, I'm making a tool which will be in use by a range of people, so making the tool accept inputs with \n or not would be the better choice than getting everyone to ensure they dont have \n at the end of every file.
Dec 19 2014
next sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Fri, 19 Dec 2014 10:22:01 +0000
Colin via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

 On Thursday, 18 December 2014 at 22:29:30 UTC, Ali =C3=87ehreli wrote:
 On 12/18/2014 02:51 AM, Colin wrote:

 vi, and it does indeed have a '\n' at the end of file.
Ah, I see. That's a little annoying.
It looks like there are ways of dealing with it: http://stackoverflow.com/questions/1050640/vim-disable-automatic-newlin=
e-at-end-of-file
 Ali
 "happy with Emacs :p"
=20 Does emacs do this aswell? :) =20 Anyway, I'm making a tool which will be in use by a range of people, so making the tool accept inputs with \n or not would be the better choice than getting everyone to ensure they dont have \n at the end of every file.
`.strip` the read string. or just `.stripRight`. this will remove all unnecessary whitespace noise at both sides of string (`.strip`) or only at the end of the string (`.stripRight`).
Dec 19 2014
prev sibling next sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Fri, 19 Dec 2014 10:22:01 +0000
Colin via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

 On Thursday, 18 December 2014 at 22:29:30 UTC, Ali =C3=87ehreli wrote:
 On 12/18/2014 02:51 AM, Colin wrote:

 vi, and it does indeed have a '\n' at the end of file.
Ah, I see. That's a little annoying.
It looks like there are ways of dealing with it: http://stackoverflow.com/questions/1050640/vim-disable-automatic-newlin=
e-at-end-of-file
 Ali
 "happy with Emacs :p"
=20 Does emacs do this aswell? :) =20 Anyway, I'm making a tool which will be in use by a range of people, so making the tool accept inputs with \n or not would be the better choice than getting everyone to ensure they dont have \n at the end of every file.
p.s. in D string slicing cost almost nothing and it doesn't need to scan the whole string to find it's length, so stripping is very cheap.
Dec 19 2014
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 12/19/2014 02:22 AM, Colin wrote:

 On Thursday, 18 December 2014 at 22:29:30 UTC, Ali Çehreli wrote:
 "happy with Emacs :p"
Does emacs do this aswell? :)
Emacs can and does do everything: :) http://www.gnu.org/software/emacs/manual/html_node/emacs/Customize-Save.html Ali
Dec 23 2014
prev sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Thursday, 18 December 2014 at 09:18:36 UTC, Colin wrote:
 I don't think it's my editor adding chars to the end of the 
 file, as I'm using vi.
:-) vim actually does it by default. Check out ":help eol" from inside it. I think other vi clones do it too but I'm not sure, but I know vim has it as documented behavior. There's apparently a few reasons for it: http://stackoverflow.com/questions/729692/why-should-files-end-with-a-newline
Dec 18 2014