Thread: How to go through all the files in a directory.

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    71

    How to go through all the files in a directory.

    Hi everyone,

    I am trying to go through every text file in one of my directories, all with names that vary slightly, to create a table from that information but I am not sure how to access more than 1 file.

    When you create a file pointer, you need the file name as well as the mode for the parameter, and I'd like to be able to go through all the files without having to know their names, though the only difference between the files is the time stamp that is attached to the end of the name.

    Once solution that I thought of but was not sure if works was after going through that files data, record the number of bits in that file, and increment the file pointer by that many bits.

    Is this the right approach to this problem or does this actually not work, is very crude, or there is a method many times better?
    Last edited by workisnotfun; 11-17-2012 at 01:12 PM.

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    If you don't mind going out of the realm of the C Standard, and into POSIX territory, you can use opendir(), readdir() and closedir().

    In Windows territory (Windows can also be made to be POSIX) the method may be different. I'd try a google search with MSDN in the search terms

    Your alternative does not work, at all!

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    71
    Glad I asked before trying it! The requirement is a C program however, but Bash Script might also be allowed. Any thoughts on how to do it with only these 2 languages?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    71
    The sample program that prints out all the file names seems like it can be used. Thank you for the link.

  6. #6
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    POSIX is fantastic, and provides very useful directory and file related functions.

    To implement POSIX on Windows 7, you need to install the Subsystem for UNIX-based Applications

    It only works on Windows 7, and Windows Server 2008(or Windows Server 8).
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    71
    I probably should have mentioned this program is for fedora 17.

  8. #8
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Quote Originally Posted by workisnotfun View Post
    I probably should have mentioned this program is for fedora 17.
    Then POSIX is already installed. You can view or download the GNU C Library Reference Manual(as a PDF) here: The GNU C Library - GNU Project - Free Software Foundation (FSF)

    Under the section "File System Interface" -> "Accessing Directories" section, there is documentation on opendir(), readdir() and closedir(), with sample code. It's very easy to use POSIX to work with files and directories.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    71
    That's POSIX? Dam, I kind of wanted to do this without super helpful functions that do everything for you so I'd learn more, and I already found these functions from the link Salem mentioned. If there is a more dificult way of going through text files in a specific directory with an intuitive algorithm that can be self created, I'd like to hear that but right now I am already using the opendir(),readdir() and closeddir() taken from the link (lol), though I didn't know that was POSIX until now.

    Do most linux systems have this POSIX btw?

  10. #10
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by workisnotfun View Post
    Do most linux systems have this POSIX btw?
    Yes, all Linux systems "have POSIX" (but the level of conformance to the standard varies across distros). POSIX is a set of programming interfaces, user-land utilities, etc. Linux distros almost universally implement these standard interfaces and utilities. It's how applications are programmed for the system.

    Windows has its own non-standard API's, while Linux/Unix have the standard POSIX API's. When you "install POSIX" on Windows you're installing a layer to translate the POSIX API to the non-standard Windows API. There are a number of POSIX implementations for Windows that also work on other versions of Windows than just Windows 7 or Windows Server 2008, such as Cygwin.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by workisnotfun View Post
    ... Once solution that I thought of but was not sure if works was after going through that files data, record the number of bits in that file, and increment the file pointer by that many bits.

    ... If there is a more difficult way of going through text files in a specific directory with an intuitive algorithm that can be self created, I'd like to hear that ...
    What you're aiming for is to read the file's directory and discover how files are stored (you assume adjacently), and follow along as the operating system would when it lists files. There may be some low-level disk read functions, and once you fully understand the particular operating system/version's file structure you could do that. But opendir() etc. are about as difficult as you want in practice. I understand that the specifics of Microsoft's current file system structure are a well kept secret. So unless you want to practice on old systems with simpler file systems, your more difficult, self-created approach is nearly impossible nowadays.

    On old machines going back to early 80s you could do a read-track & sector talking directly to the disk controller. Then you could assemble your own functions that would duplicate the operating system's function, plus add more features and sometimes even end up with faster code.
    Last edited by nonoob; 11-19-2012 at 02:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. All files in a directory
    By cloudy in forum Linux Programming
    Replies: 1
    Last Post: 06-09-2008, 01:50 AM
  2. files in directory
    By rajkumarmadhani in forum C Programming
    Replies: 1
    Last Post: 07-28-2007, 05:35 AM
  3. How do I all files in a directory ?
    By vnayak in forum C Programming
    Replies: 2
    Last Post: 07-03-2007, 02:01 AM
  4. How to use files in other directory?
    By zaracattle in forum C++ Programming
    Replies: 4
    Last Post: 10-01-2006, 04:37 PM
  5. Files in A Directory
    By devour89 in forum C++ Programming
    Replies: 4
    Last Post: 01-26-2003, 08:39 AM