Thread: getting filenames

  1. #1
    Registered User madsmile's Avatar
    Join Date
    Feb 2002
    Posts
    26

    getting filenames

    How do i get all the filenames from a specific directory?

    example:
    C:\madsmile\ <- my prog root dir
    C:\madsmile\saves\ <- my save directory

    i want to get all the filenames in "saves" directory, but i need to the path to be relative to the root dir... so its going to be always "saves\"... any body got a clue how can i do that??

    thx
    MADSmile
    ICQ #3653692
    (i'm running Borlad C++ Ver 3.1 under MSDOS)

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    What compiler/os?

  3. #3
    Registered User madsmile's Avatar
    Join Date
    Feb 2002
    Posts
    26
    Borland C++ Version 3.1 (MsDOS)
    MADSmile
    ICQ #3653692
    (i'm running Borlad C++ Ver 3.1 under MSDOS)

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    These two might help you, taken directly from the help files:


    findfirst


    Syntax

    #include <dir.h>
    int findfirst(const char *pathname, struct ffblk *ffblk, int attrib);

    Description

    Searches a disk directory.
    findfirst begins a search of a disk directory for files specifed by attributes or wildcards.
    pathname is a string with an optional drive specifier path and file name of the file to be found. Only the file name portion can contain wildcard match characters (such as ? or *). If a matching file is found the ffblk structure is filled with the file-directory information.

    Win16

    For Win16, the format of the structure ffblk is as follows:

    struct ffblk {
    char ff_reserved[21]; /* reserved by DOS */
    char ff_attrib; /* attribute found */
    int ff_ftime; /* file time */
    int ff_fdate; /* file date */
    long ff_fsize; /* file size */
    char ff_name[13]; /* found file name */
    };

    Win32

    For Win32, the format of the structure ffblk is as follows:

    struct ffblk {
    long ff_reserved;
    long ff_fsize; /* file size */
    unsigned long ff_attrib; /* attribute found */
    unsigned short ff_ftime; /* file time */
    unsigned short ff_fdate; /* file date */
    char ff_name[256]; /* found file name */
    };

    attrib is a file-attribute byte used in selecting eligible files for the search. attrib should be selected from the following constants defined in dos.h:

    FA_RDONLY Read-only attribute
    FA_HIDDEN Hidden file
    FA_SYSTEM System file
    FA_LABEL Volume label
    FA_DIREC Directory
    FA_ARCH Archive

    A combination of constants can be ORed together.
    For more detailed information about these attributes refer to your operating system documentation.
    ff_ftime and ff_fdate contain bit fields for referring to the current date and time. The structure of these fields was established by the operating system. Both are 16-bit structures divided into three fields.

    ff_ftime:

    Bits 0 to 4 The result of seconds divided by 2 (for example 10 here means 20 seconds)
    Bits 5 to 10 Minutes
    Bits 11 to 15 Hours

    ff_fdate:

    Bits 0-4 Day
    Bits 5-8 Month
    Bits 9-15 Years since 1980 (for example 9 here means 1989)

    The structure ftime declared in io.h uses time and date bit fields similar in structure to ff_ftime and ff_fdate.

    Return Value

    findfirst returns 0 on successfully finding a file matching the search pathname.
    When no more files can be found, or if there is an error in the file name:

    -1 is returned
    errno is set to

    ENOENT Path or file name not found

    _doserrno is set to one of the following values:

    ENMFILE No more files

    ENOENT Path or file name not found

    Borland C++ 5.0 Programmer's Guide




    findnext


    Syntax

    #include <dir.h>
    int findnext(struct ffblk *ffblk);

    Description

    Continues findfirst search.
    findnext is used to fetch subsequent files that match the pathname given in findfirst. ffblk is the same block filled in by the findfirst call. This block contains necessary information for continuing the search. One file name for each call to findnext will be returned until no more files are found in the directory matching the pathname.

    Return Value

    findnext returns 0 on successfully finding a file matching the search pathname. When no more files can be found or if there is an error in the file name

    -1 is returned
    errno is set to

    ENOENT Path or file name not found

    _doserrno is set to one of the following values:

    ENMFILE No more files

    ENOENT Path or file name not found

    Borland C++ 5.0 Programmer's Guide
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Oh, BTW

    I made a small sample program. This searches the current directory (where the EXE-file is) for bitmaps.
    If you want to search through a sub directory, change findfirst("*.bmp", &Holder, 0); to findfirst("Subdir/*.bmp", &Holder, 0);

    Code:
    #include <dir.h>
    #include <iostream.h>
    #include <conio.h>
    
    int main()
    {
       struct ffblk Holder;
       int Status;
       int i=0;
    
       //Search for bitmaps and print them out
       Status=findfirst("*.bmp", &Holder, 0);
       while(!Status)
       {
          cout << Holder.ff_name << endl;
          Status=findnext(&Holder);
          i++;
    
          //Print one page at a time
          if(i%25==0) getch();
       }
       getch();
    
       //Exit
       return 0;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting filenames from a file
    By vinsta18 in forum C++ Programming
    Replies: 1
    Last Post: 01-12-2005, 11:52 AM
  2. long filenames
    By surdy in forum C Programming
    Replies: 2
    Last Post: 09-22-2004, 12:25 PM
  3. reading filenames
    By Paul in forum C++ Programming
    Replies: 1
    Last Post: 07-01-2003, 10:02 AM
  4. Replies: 2
    Last Post: 04-14-2002, 01:03 PM
  5. Sequential Filenames?
    By Confused in forum C Programming
    Replies: 5
    Last Post: 11-17-2001, 12:28 PM