findfirst/findnext in Turbo C

Bob Stout Bob.Stout at p6.f506.n106.z1.fidonet.org
Wed Oct 25 14:24:57 AEST 1989


In an article of <23 Oct 89 13:12:05 GMT>, (Doug Krause) writes:

 >I'm using Turbo C 2.0 on a PC clone.  My question:  I'm using findfirst
 >and findnext to read the disk directory.  In the structure returned are
 >two integers that tell the file date and file time.  Is there a function
 >to convert these numbers into something more useful like hh:mm?

------------------------------- Cut here ------------------------------------
/*  Sample file date and time display.*/
 
#include <stdio.h>
#include <dos.h>
 
struct DOS_TIME
{
        unsigned int ss : 5;
        unsigned int mm : 6;
        unsigned int hh : 5;
} ;
#define dos_time(t) (*(struct DOS_TIME *)(&(t)))
 
struct DOS_DATE
{
        unsigned int da : 5;
        unsigned int mo : 4;
        unsigned int yr : 7;
} ;
#define dos_date(t) (*(struct DOS_DATE *)(&(t)))
 
main(int argc, char *argv[])
{
#ifdef __ZTC__                                  /* Zortech C/C++        */
        struct FIND *ffblk;
#else                                           /* TC/MSC               */
        struct find_t *ffblk =
                (struct find_t)malloc(sizeof(struct find_t));
#endif
 
        if (2 > argc)
        {
                puts("\aUsage: SHOWDATE filename[.ext]");
                exit(1);
        }
#ifdef __ZTC__
        if (!(ffblk = findfirst(argv[1], 0xff)))
#elif defined(__TURBOC__)
        if (findfirst(argv[1], ffblk, 0xff)
#else                                           /* MSC/QC               */
        if (_dos_findfirst(argv[1], 0xff, ffblk)
#endif
        {
                printf("\aCant find %s\n", argv[1]);
                exit(2);
        }
        printf("File date is %d-%d-%d\n",
                dos_date(ffblk->date).mo,
                dos_date(ffblk->date).da,
                (dos_date(ffblk->date).yr + 80) % 100);
        printf("File time is %d:%d:%d\n",
                dos_time(ffblk->time).hh,
                dos_time(ffblk->time).mm,
                dos_time(ffblk->time).ss);
}
-----------------------------------------------------------------------------
Please note that I don't have TC up on this machine, so the findfirst syntax  
is from memory. Finally, you can also use TC's getftime() function and/or its  
ftime structure, e.g.
------------------------------- Cut here ------------------------------------
        union {
                struct ftime ftp;
                struct {
                        unsigned time,
                                 date;
                } detail;
        } stamp;

        stamp.detail.time = time_from_findfirst;
        stamp.detail.date = date_from_findfirst;

        printf("%d-%d-%d %2d:%02d:%02d\n", stamp.ftp.ft_month,                   
stamp.ftp.ft_day, stamp.ftp.ft_year + 80, stamp.ftp.ft_hour,                   
stamp.ftp.ft_min, stamp.ftp.ft_tsec * 2);
-----------------------------------------------------------------------------
Of course all this is highly non-portable, but then it *is* OS-specific... 



More information about the Comp.lang.c mailing list