Thread: File names...

  1. #1
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273

    File names...

    Hi, I was wondering if there's a way to list the names of some files in a folder. I was thinking about doing something like

    system ("cd fildfile");
    system ("dir");

    but all I want is the filenames, not the complete info of each file. The files will have a max of 10 letters.

    I'm using ms vc and windows xp

    thanks!!

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Or even, novel concept this, the FAQ

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    That's fantastic, but there isn't any simple function that'll do it is there? I also got this message when I tried to run it:

    return.exe has encountered a problem and needs to close. We are sorry for the inconvenience.
    even though:

    --------------------Configuration: return - Win32 Debug--------------------
    Compiling...
    return.c
    Linking...

    return.exe - 0 error(s), 0 warning(s)

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    salem, why does it say "banned" under your name?

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Hmmm ... it doesn't seem to recognise dir.h ... any ideas???

    --------------------Configuration: file - Win32 Debug--------------------
    Compiling...
    file.c
    u:\file.c(2) : fatal error C1083: Cannot open include file: 'dir.h': No such file or directory
    Error executing cl.exe.

    file.obj - 1 error(s), 0 warning(s)

  7. #7
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    You've taken the first example because you use a Borland Compiler, right?

  8. #8
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Quote Originally Posted by twomers
    Hmmm ... it doesn't seem to recognise dir.h ... any ideas???
    I wrote you a quick example, under windows you have to use FindFirstFile and FindNextFile, but like all windows API functions it is hardly intuitive also I don't see why they couldn't just make it one function.

    Code:
    #include <stdio.h>
    #include <windows.h>
    
    /* 
     * listfiles.c, an example of listing files
     * in a directory using Windows API.
     */
    
    int main(void)
    {
        WIN32_FIND_DATA ffd;
        char dir[] = "c:\\program files\\*"; /* '*' is a wildcard character
                                                it tells FindFirstFile to
                                                find any file in the directory */
        HANDLE h = FindFirstFile(dir, &ffd);
        if (h == INVALID_HANDLE_VALUE) {
            printf("Unable to list files.");
            return 0;
        }
        
        printf("Listing files in directory %s\n", dir);
        do {
            printf("%s\n", ffd.cFileName); /* ffd.cFileName is the name of the
                                              file found */
        } while (FindNextFile(h, &ffd) != 0);
        
        
        return 0;
    }

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Quote Originally Posted by TactX
    You've taken the first example because you use a Borland Compiler, right?
    what's a borland compiler??

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    That's a fantastic piece of code!! and it works

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by twomers
    what's a borland compiler??
    its a compiler which actually compiles C/C++ programs
    http://www.borland.com/us/products/index.html


    ssharish2005

  12. #12
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> and it works

    It may work as a small isolated program, however, as production code or as part of a larger program that is doing this a lot, it will cause problems. It does not, for example, close the search handle which it should.

    My tutorial starting here addresses this and other issues.

    It is written in C++ but if you change the couts to printfs it will work, the API calls and returns will be the same.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  13. #13
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Quote Originally Posted by adrianxw
    >>> and it works

    It may work as a small isolated program, however, as production code or as part of a larger program that is doing this a lot, it will cause problems. It does not, for example, close the search handle which it should.

    Oops my bad, I only whipped it up in a couple of minutes. Just pop a FindClose(h); in there before return 0. Oh yeah and there's no need for my string to be a character array too I guess.
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    /* 
     * listfiles.c, an example of listing files
     * in a directory using Windows API.
     */
    
    int main(void)
    {
        WIN32_FIND_DATA ffd;
        char *dir = "c:\\winnt\\*"; /* '*' is a wildcard character */
        HANDLE h = FindFirstFile(dir, &ffd);
    
        if (h == INVALID_HANDLE_VALUE) {
            printf("Unable to list files.");
            return 0;
        }
        
        printf("Listing files in directory %s\n", dir);
        do {
            printf("%s\n", ffd.cFileName);
        } while (FindNextFile(h, &ffd) != 0);
        
        FindClose(h);
        return 0;
    }
    Last edited by Brian; 01-03-2006 at 02:41 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File being filled with NULLs
    By Tigers! in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2009, 05:28 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM