Newbie's question: I want to ls the filenames of a directory. The program in
C is something like:
#include
int main(){
DIR *dp;
struct dirent *dirp;
dp = opendir("/");
while((dirp = readdir(dp)) != NULL)
printf("%s\n", dirp->d_name);
closedir(dp);
}
and I write that in has
Hello Albert,
Monday, April 9, 2007, 6:46:14 AM, you wrote:
> It can print the first entry of the dir, but how can we list it all
> like the C prog? map ? list comperhension?
http://haskell.org/haskellwiki/IO_inside forever :)
--
Best regards,
Bulatmailto:[EMAIL
Albert Lee wrote:
> and I write that in haskell:
>
> -
> import System.Posix
> import System.IO
>
> main = do
> dp <- openDirStream "/"
> df <- readDirStream dp
> putStrLn df
> closeDirStream dp
Some code snippets out of harchive, which uses DirStream:
bracket (openDirStr
It looks like all you can do with DirStream is get the filename, not
look at any other fields of the dirent - actually, it seems name
is the only standard field. You might as well use
getDirectoryContents, unless you have a directory so huge that
a list of all the filenames takes too much memory!
Hello,
This multipart tutorial seems similar to what you are describing:
http://blog.moertel.com/articles/2007/03/28/directory-tree-printing-in-haskell-part-three-lazy-i-o
The tutorial is actually a bit more complicated, it is traversing a
whole directory tree and printing a nice graph.
HTH,
j
Albert Lee on 2007-04-09 10:46:14 +0800:
> I want to ls the filenames of a directory.
[...]
> and I write that in haskell:
>
> -
> import System.Posix
> import System.IO
>
> main = do
> dp <- openDirStream "/"
> df <- readDirStream dp
> putStrLn df
> closeDirStream dp
>
Thanks for your reply. and I have done it by:
import System.IO
import System.Posix
import System.Directory
show_current_dir :: IO ()
show_current_dir = do
curr_dir <- getWorkingDirectory
putStrLn ("Current Working Directory:" ++ curr_dir)
ls_dir1 :: String -> IO [()]
ls_dir1 fp = do
files <
On 09/04/07, Albert Lee <[EMAIL PROTECTED]> wrote:
mapM putStrLn files
Seeing as you're not doing anything with the results of this map, you
probably want to use mapM_ instead. Then the result type of ls_dir1
can be IO (), which is neater.
--
-David House, [EMAIL PROTECTED]
__