digitalmars.D.learn - Read text file, line by line?
- AEon (13/13) Mar 19 2005 It seems *very* easy to open a text file in D, see below code (snipped f...
- Martin Boeker (14/31) Mar 19 2005 For example, you could do something like this:
- Derek Parnell (45/80) Mar 19 2005 Here is the code I used in the 'Build' utility...
- Walter (7/8) Mar 19 2005 The common line ending conventions are:
- Derek Parnell (12/23) Mar 19 2005 I normally would agree, however the word "convention" was mentioned. ;-)
- Derek Parnell (8/31) Mar 19 2005 (Oops. Left out the last line of my reply)
- J C Calvarese (8/28) Mar 19 2005 Also, I think most users appreciate it when a program doesn't explode
- AEon (5/19) Mar 19 2005 Actually the programming editor UltraEdit lets you do that was well. I.e...
- Iain S (2/2) Dec 13 2011 How would one achieve this in D2? I have tried for a couple of hours
- Robert Clipsham (10/12) Dec 13 2011 import std.stdio;
- Jonathan M Davis (5/16) Dec 13 2011 However, note that the buffer is reused between loop iterations, so if y...
- Kagamin (3/13) Dec 14 2011 AFAIK it used to preserve EOL in the lines. Was it fixed?
- bearophile (5/6) Dec 14 2011 It keeps them on default, but there is a way to not keep them:
- Walter (4/6) Mar 19 2005 line
- AEon (17/25) Mar 19 2005 Aha... I compiled a .doc file from the latest online info, but the std.s...
- J C Calvarese (19/56) Mar 19 2005 You mean the material in http://www.digitalmars.com/d/std_string.html?
- AEon (12/39) Mar 19 2005 I must have copy/pasted an old part. The links to that page (ironically)...
- Derek Parnell (8/15) Mar 20 2005 I've just had a look at std.string.splitlines and it does do what I need...
- Walter (7/18) Mar 20 2005 the
- Stewart Gordon (11/13) Mar 21 2005 Do you mean splitLines or splitlines?
- David Medlock (7/24) Mar 03 2007 Is this what you need?
- David Medlock (3/37) Mar 03 2007 OOps. Fresh install of newsreader...ignore.
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
Mar 19 2005
For example, you could do something like this: import std.stream; void readfile(char[] fn){ File f = new File(); char[] l; f.open(fn); while(!f.eof()) { l = f.readLine(); printf("line: %.*s\n", l); } f.close(); } Martin 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
Mar 19 2005
On Sun, 20 Mar 2005 00:46:10 +0100, Martin Boeker wrote:For example, you could do something like this: import std.stream; void readfile(char[] fn){ File f = new File(); char[] l; f.open(fn); while(!f.eof()) { l = f.readLine(); printf("line: %.*s\n", l); } f.close(); } Martin AEon wrote:Here is the code I used in the 'Build' utility... // Read an entire file into a string. char[] GetFileText(char[] pFileName) { char[] lFileText; if (! std.file.exists( pFileName)) { return ""; } else { lFileText = cast(char[]) std.file.read(pFileName); // Ensure it ends with a EOL marker. if ( (lFileText.length == 0) || (lFileText[$-1] != '\n')) lFileText ~= std.path.linesep; return lFileText; } } // Read a entire file in to a set of lines (strings). char[][] GetFileTextLines(char[] pFileName) { char[][] lLines; char[] lText; char[] lDelim; if (! std.file.exists(pFileName)) { throw new Exception( std.string.format("File '%s' not found.", pFileName)); } else { lText = GetFileText(pFileName); // Set the EOL marker based on ones already used in the file. if (std.string.find(lText, "\r\n") != -1 ) lDelim = "\r\n"; else lDelim = "\n"; lLines = split( lText, lDelim); return lLines; } } -- Derek Parnell Melbourne, Australia 20/03/2005 11:19:46 AMIt 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
Mar 19 2005
"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 so I suggest using std.string.splitLines() instead.
Mar 19 2005
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...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. -- Derek Parnell Melbourne, Australia 20/03/2005 1:10:52 PMHere is the code I used in the 'Build' utility...The common line ending conventions are: \n unix \r\n windows \r mac so I suggest using std.string.splitLines() instead.
Mar 19 2005
On Sun, 20 Mar 2005 13:14:33 +1100, Derek Parnell wrote:On Sat, 19 Mar 2005 16:43:54 -0800, Walter wrote:(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. -- Derek Parnell Melbourne, Australia 20/03/2005 1:18:16 PM"Derek Parnell" <derek psych.ward> wrote in message news:1p89hc3q3x38p$.18njvr6lvkb5x.dlg 40tude.net...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.Here is the code I used in the 'Build' utility...The common line ending conventions are: \n unix \r\n windows \r mac so I suggest using std.string.splitLines() instead.
Mar 19 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/
Mar 19 2005
Derek Parnell says...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. AEonThe 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.
Mar 19 2005
How would one achieve this in D2? I have tried for a couple of hours now and have achieved nothing but stress.
Dec 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/
Dec 13 2011
On Tuesday, December 13, 2011 14:08:14 Robert Clipsham wrote:On 13/12/2011 13:58, Iain S wrote: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 DavisHow 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); } }
Dec 13 2011
On Tuesday, 13 December 2011 at 14:08:15 UTC, Robert Clipsham wrote:On 13/12/2011 13:58, Iain S wrote:AFAIK it used to preserve EOL in the lines. Was it fixed?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); } }
Dec 14 2011
Kagamin:AFAIK it used to preserve EOL in the lines. Was it fixed?It keeps them on default, but there is a way to not keep them: http://www.dlang.org/phobos/std_stdio.html#ByLine Bye, bearophile
Dec 14 2011
"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 thelineby line checking by hand?You can use std.string.splitLines() to turn it into an array of lines.
Mar 19 2005
Walter says...Aha... I compiled a .doc file from the latest online info, but the std.string lib was not there, so I missed those infos. Is the std.string lib info new? BTW: I started to port AEstats... the code is so clean, and neat, and simple to code, it makes my cry with happiness. Thanx for D... *snif*. ANSI C I will revisit never again!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.The common line ending conventions are:\n unix \r\n windows \r macso I suggest using std.string.splitLines() instead.Found that out the hard way when coding AEstats under windows vs. linux. Does D have a EOL or so variable that would point to the right escape chars, depending on the OS? I noted that "import std.path;" has several vars like "sep" = "\" (windows) and probable "\" under linux. If something like eof does not exist, I am sure it would be very helpful, when trying to write portable code. Martin and Derek, thanx for those real world examples, will look into them. AEon
Mar 19 2005
AEon wrote:Walter says...You mean the material in http://www.digitalmars.com/d/std_string.html? It's been there for at least a couple years. I wouldn't call it new. ;) (It did used to be part of http://www.digitalmars.com/d/phobos.html, though. I guess it got long enough to warrant a separate page.)Aha... I compiled a .doc file from the latest online info, but the std.string lib was not there, so I missed those infos. Is the std.string lib info new?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.BTW: I started to port AEstats... the code is so clean, and neat, and simple to code, it makes my cry with happiness. Thanx for D... *snif*. ANSI C I will revisit never again!Another C user converted. Yay!The common line ending conventions are:\n unix \r\n windows \r macso I suggest using std.string.splitLines() instead.Found that out the hard way when coding AEstats under windows vs. linux.Does D have a EOL or so variable that would point to the right escape chars, depending on the OS?Apparently, std.string doesn't have an equivalent function. I don't really see why you'd want it since we already have readLine and splitLines. It guess it wouldn't hurt to add such a function.I noted that "import std.path;" has several vars like "sep" = "\" (windows) and probable "\" under linux. If something like eof does not exist, I am sure it would be very helpful, when trying to write portable code.(I think you mean to write "eol" instead of "eof" here.) I don't know the ins and outs of writing cross-platform code. How does it usually break down? Window: \r\n Linux: \n MacOS: \r Does using \r\n for EOL typically fail to work properly on Linux and MacOS? -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Mar 19 2005
J C Calvarese says...I must have copy/pasted an old part. The links to that page (ironically) where in the doc :)... my jaw dropped when I saw all those functions... snif... this will make re-writing so much more easy. I noted regular expressions as well, possibly that would make all my tedious parsing, almost trivial... :)Is the std.string lib info new?You mean the material in http://www.digitalmars.com/d/std_string.html? It's been there for at least a couple years. I wouldn't call it new. ;) (It did used to be part of http://www.digitalmars.com/d/phobos.html, though. I guess it got long enough to warrant a separate page.)Another C user converted. Yay!I would have converted much earlier, hat I known about D. But I seem to be priviledged that most of D in "there" already.That was just a thought, I usually think in patterns, and when I seem something already implemented, and then something similar comes up, I take note :) Will look into those funtions.The common line ending conventions are:\n unix \r\n windows \r macso I suggest using std.string.splitLines() instead.Found that out the hard way when coding AEstats under windows vs. linux.Does D have a EOL or so variable that would point to the right escape chars, depending on the OS?Apparently, std.string doesn't have an equivalent function. I don't really see why you'd want it since we already have readLine and splitLines. It guess it wouldn't hurt to add such a function.Ops, yep EOL. AEonI noted that "import std.path;" has several vars like "sep" = "\" (windows) >> and probable "\" under linux. If something like eof does not exist, I am sure it would be very helpful, >> >> when trying to write portable code.(I think you mean to write "eol" instead of "eof" here.) I don't know the ins and outs of writing cross-platform code. How does it usually break down?
Mar 19 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...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 AMDoes one need to parse the file as one char[] array block, and do all thelineby line checking by hand?You can use std.string.splitLines() to turn it into an array of lines.
Mar 20 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:the"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 allLine 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!lineI'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 ;-)by line checking by hand?You can use std.string.splitLines() to turn it into an array of lines.
Mar 20 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.
Mar 21 2005
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? AEonIs 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
Mar 03 2007
David Medlock wrote:AEon wrote:OOps. Fresh install of newsreader...ignore. Hehe.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? AEonIs 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
Mar 03 2007