March 20, 2005
Derek Parnell wrote:
> On Sun, 20 Mar 2005 13:14:33 +1100, Derek Parnell wrote:
> 
> 
>>On Sat, 19 Mar 2005 16:43:54 -0800, Walter wrote:
>>
>>
>>>"Derek Parnell" <derek@psych.ward> wrote in message
>>>news:1p89hc3q3x38p$.18njvr6lvkb5x.dlg@40tude.net...
>>>
>>>>Here is the code I used in the 'Build' utility...
>>>
>>>The common line ending conventions are:
>>>
>>>    \n    unix
>>>    \r\n    windows
>>>    \r    mac
...


> (Oops.  Left out the last line of my reply)
> 
> So I no longer assume that because I'm running in a <whatever> environment
> that the file was *saved* using the <whatever> convention.

Also, I think most users appreciate it when a program doesn't explode just because they're trying to input a Unix text file on their Windows PC. It's those little things that make the difference.

-- 
Justin (a/k/a jcc7)
http://jcc_7.tripod.com/d/
March 20, 2005
Derek Parnell says...
>> The common line ending conventions are:
>> 
>>     \n    unix
>>     \r\n    windows
>>     \r    mac
>> 
>> so I suggest using std.string.splitLines() instead.
>
>I normally would agree, however the word "convention" was mentioned. ;-)
>
>Initially, this is how I wrote it the routines, that is, I assumed the conventions. However, what I discovered was that it is quite possible to have a Unix-EOL file in a Windows environment (eg. One gotten via ftp from a Unix system, or one saved by your Windows editor as a 'Unix' file.)
>
>So, I instead opted to examine what the file was actually using in itself, before trying to split it into lines.

Actually the programming editor UltraEdit lets you do that was well. I.e. save files in Unix format. So in my really old code I checked for \r and \n, to ensure the parser would not hickup on log files.

AEon
March 21, 2005
On Sat, 19 Mar 2005 15:44:28 -0800, Walter wrote:

> "AEon" <AEon_member@pathlink.com> wrote in message news:d1id3r$273p$1@digitaldaemon.com...
>> Does one need to parse the file as one char[] array block, and do all the
> line
>> by line checking by hand?
> 
> You can use std.string.splitLines() to turn it into an array of lines.

I've just had a look at std.string.splitlines and it does do what I need, in that it doesn't assume any particular line ending convention. Thanks Walter, I'll use that from now on. Sorry for ever doubting you ;-)

-- 
Derek
Melbourne, Australia
21/03/2005 11:34:38 AM
March 21, 2005
"Derek Parnell" <derek@psych.ward> wrote in message news:i3yn7fqa6zex$.h2xheiurc8r5$.dlg@40tude.net...
> On Sat, 19 Mar 2005 15:44:28 -0800, Walter wrote:
>
> > "AEon" <AEon_member@pathlink.com> wrote in message news:d1id3r$273p$1@digitaldaemon.com...
> >> Does one need to parse the file as one char[] array block, and do all
the
> > line
> >> by line checking by hand?
> >
> > You can use std.string.splitLines() to turn it into an array of lines.
>
> I've just had a look at std.string.splitlines and it does do what I need, in that it doesn't assume any particular line ending convention. Thanks Walter, I'll use that from now on. Sorry for ever doubting you ;-)

Line end parsing is one of those routine chores that everyone gets wrong, that's why it needs to be a standard library function. splitLines() is wrong, too, it needs to be fixed to recognize unicode LS and PS, but if everyone uses splitLines(), then they'll automatically get fixed as well!


March 21, 2005
Walter wrote:
<snip>
> You can use std.string.splitLines() to turn it into an array of lines.

Do you mean splitLines or splitlines?

Guess it's another reason to follow conventions - it helps you to remember what you called stuff.  I bet Sun is having quite a bit of trouble....

http://www.mindprod.com/jgloss/gotchas.html#INCONSISTENCIES

Stewart.

-- 
My e-mail is valid but not my primary mailbox.  Please keep replies on the 'group where everyone may benefit.
March 03, 2007
AEon wrote:
> It seems *very* easy to open a text file in D, see below code (snipped from the
> wordcount example from the documentation).
> 
> <code>
>    import std.file;
>    // Open/read complete file into a D string! Nice.	
>    char[] input;
>    input = cast(char[])std.file.read(arg);		
> </code>
> 
> I checked the other command in std.file. AFAICT there seems to be no way to open
> a file handle, and then read *line by line* from a e.g. config file?
> 
> Does one need to parse the file as one char[] array block, and do all the line
> by line checking by hand?
> 
> AEon

Is this what you need?



import std.stream;

auto x = new File( filename, FileMode.In );
scope(exit) { x.close(); }
foreach( char[] line; x ) writefln( line );

-DavidM

March 03, 2007
David Medlock wrote:
> AEon wrote:
>> It seems *very* easy to open a text file in D, see below code (snipped from the
>> wordcount example from the documentation).
>>
>> <code>
>>    import std.file;
>>    // Open/read complete file into a D string! Nice.      char[] input;
>>    input = cast(char[])std.file.read(arg);       </code>
>>
>> I checked the other command in std.file. AFAICT there seems to be no way to open
>> a file handle, and then read *line by line* from a e.g. config file?
>>
>> Does one need to parse the file as one char[] array block, and do all the line
>> by line checking by hand?
>>
>> AEon
> 
> Is this what you need?
> 
> 
> 
> import std.stream;
> 
> auto x = new File( filename, FileMode.In );
> scope(exit) { x.close(); }
> foreach( char[] line; x ) writefln( line );
> 
> -DavidM
> 
OOps. Fresh install of newsreader...ignore.

Hehe.
December 13, 2011
How would one achieve this in D2?  I have tried for a couple of hours now and have achieved nothing but stress.
December 13, 2011
On 13/12/2011 13:58, Iain S wrote:
> How would one achieve this in D2?  I have tried for a couple of hours
> now and have achieved nothing but stress.

import std.stdio;

void main() {
    foreach(line; File("myTextFile.txt").byLine()) {
        writefln(line);
    }
}

-- 
Robert
http://octarineparrot.com/
December 13, 2011
On Tuesday, December 13, 2011 14:08:14 Robert Clipsham wrote:
> On 13/12/2011 13:58, Iain S wrote:
> > How would one achieve this in D2?  I have tried for a couple of hours now and have achieved nothing but stress.
> 
> import std.stdio;
> 
> void main() {
>      foreach(line; File("myTextFile.txt").byLine()) {
>          writefln(line);
>      }
> }

However, note that the buffer is reused between loop iterations, so if you're trying to keep any part of the line around after an iteration, you're going to need to dup or idup it.

- Jonathan M Davis