digitalmars.D - How does listdir work?
- Joey Peters (14/14) Sep 07 2004 I'm making my own (small) build system and when I wanted to write someth...
- J C Calvarese (21/40) Sep 07 2004 I think there's a bug in getcwd. This example works around the bug:
- Joey Peters (1/21) Sep 08 2004
- J C Calvarese (28/60) Sep 21 2004 *Update*
- Russ Lewis (10/22) Sep 21 2004 I just can't express how much I like foreach. It makes things so nice!
I'm making my own (small) build system and when I wanted to write something to autogenerate the make scripts it seemed that listdir didn't work quite the way I expected it to work. Phobos claims to just return the contents of a directory. When I tried: char[][] files = listdir(getcwd()); foreach(char[] file; files) { std.stream.stdout.writeLine(file); } It seems to only return the relative path name instead of the directory contents. ~ "/*" doesn't work either, ~"\\" neither on that case... Anyway, does anyone know how to fix this problem, or have a work around? I want to keep it portable. -Joey
Sep 07 2004
Joey Peters wrote:I'm making my own (small) build system and when I wanted to write something to autogenerate the make scripts it seemed that listdir didn't work quite the way I expected it to work. Phobos claims to just return the contents of a directory. When I tried: char[][] files = listdir(getcwd()); foreach(char[] file; files) { std.stream.stdout.writeLine(file); } It seems to only return the relative path name instead of the directory contents. ~ "/*" doesn't work either, ~"\\" neither on that case... Anyway, does anyone know how to fix this problem, or have a work around? I want to keep it portable. -JoeyI think there's a bug in getcwd. This example works around the bug: import std.file; import std.stdio; void main() { char[][] d; char[] cwd = getcwd(); cwd = cwd[0..cwd.length-1]; writef("cwd: %s\n\n", cwd); d = listdir(cwd); for(int i; i<d.length; i++) writef("%s\n", d[i]); } Apparently, getcwd returns a string contains a trailing null. This null seems to throw off listdir. Also, you might want to use std.recls to traverse directories: http://www.dsource.org/tutorials/index.php?show_example=27 -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Sep 07 2004
I think there's a bug in getcwd. This example works around the bug: import std.file; import std.stdio; void main() { char[][] d; char[] cwd = getcwd(); cwd = cwd[0..cwd.length-1]; writef("cwd: %s\n\n", cwd); d = listdir(cwd); for(int i; i<d.length; i++) writef("%s\n", d[i]); } Apparently, getcwd returns a string contains a trailing null. This null seems to throw off listdir. Also, you might want to use std.recls to traverse directories: http://www.dsource.org/tutorials/index.php?show_example=27Thanks that worked fine :)-- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Sep 08 2004
Joey Peters wrote:*Update* Now that Walter fixed the getcwd() bug with DMD 0.102, this is the proper code: import std.file; import std.stdio; void main() { char[][] d; char[] cwd = getcwd(); writef("cwd: %s\n\n", cwd); d = listdir(cwd); for(int i; i<d.length; i++) writef("%s\n", d[i]); } Also, this code based on the original post works: import std.file; import std.stream; void main() { char[][] files = listdir(getcwd()); foreach(char[] file; files) { std.stream.stdout.writeLine(file); } }I think there's a bug in getcwd. This example works around the bug: import std.file; import std.stdio; void main() { char[][] d; char[] cwd = getcwd(); cwd = cwd[0..cwd.length-1]; writef("cwd: %s\n\n", cwd); d = listdir(cwd); for(int i; i<d.length; i++) writef("%s\n", d[i]); } Apparently, getcwd returns a string contains a trailing null. This null seems to throw off listdir.-- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/Also, you might want to use std.recls to traverse directories: http://www.dsource.org/tutorials/index.php?show_example=27Thanks that worked fine :)-- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Sep 21 2004
J C Calvarese wrote:Also, this code based on the original post works: import std.file; import std.stream; void main() { char[][] files = listdir(getcwd()); foreach(char[] file; files) { std.stream.stdout.writeLine(file); } }I just can't express how much I like foreach. It makes things so nice! I'd prefer to write the code above as this, which I think is a little simpler and easier to read: void main() { foreach(char[] file; listdir(getcwd)) std.stream.stdout.writeLine(file); } It's beautiful...
Sep 21 2004