Finding a directory, and printing whats in it

This is a discussion on Finding a directory, and printing whats in it within the C Programming forums, part of the General Programming Boards category; I am trying to write a C program that receives the name of a directory, and then prints all the ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    2

    Finding a directory, and printing whats in it

    I am trying to write a C program that receives the name of a directory, and then prints all the files and directories under that directory.

    I am having a really hard time doing this. I have gotten my program to receive the string of which directory to find, but I am not sure how to display everything.

    I've looked around on the web, and found the dirent.h. But I am not sure how to use those functions, or even if they are the ones I should be using. sys/types.h is another one that I have been looking into as well.

    This program shouldn't be hard for me, but for some reason I just can't figure it out.

    Anybody out there have any advice or help to offer me?
    Thanks ahead of time.

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    well this is operating system dependent, sounds like you are using linux, might want to look here
    http://faq.cprogramming.com/cgi-bin/...=1045780608#05

    and here to change directories

    http://faq.cprogramming.com/cgi-bin/...=1045780608#02
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    That would depend on the OS environment you are using. If you are doing this in Windows you can use _findfirst() _findnext() functions declared in io.h you will also need to use a _finddata_t struct instance to find the files and directories.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    2
    yes, i am using unix. thank you for your help, those links helped me out a lot. but now it draws up another question from me:

    Is there a way for my program to determine the difference between files and directories while printing them out?

    Effectively, I would like to have my program print out all the files and directories in the current directory, and then state whether each is a file, or a directory. Any ideas?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is there a way for my program to determine the difference between files and directories while printing them out?
    Of course:
    Code:
    #include <sys/stat.h>
    
    int isDir ( char *path )
    {
      struct stat s;
    
      if ( stat ( path, &s ) != -1 )
        return S_ISDIR ( s.s_mode );
      return 0;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 12:57 PM
  2. Non-sequential printing
    By saeculum in forum C Programming
    Replies: 2
    Last Post: 03-16-2009, 06:33 AM
  3. Homework HELP - Problem Printing to file
    By brentshreve in forum C Programming
    Replies: 5
    Last Post: 04-23-2005, 02:11 AM
  4. printing directory in windows
    By hermit in forum Tech Board
    Replies: 6
    Last Post: 12-16-2004, 06:15 PM
  5. Printing list of .txt files saved in directory
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 01-15-2002, 11:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21