Skip to Content

TheBlogReaders.com

Salesforce.com, PHP, MySQL, Javascript, Ajax, Htacces

C Language Data Files Interview Questions and Answers For Freshers Part-3

Be First!

data9. How do you list a file’s date and time?

A file’s date and time are stored in the find_t structure returned from the _dos_findfirst() and_dos_findnext() functions.

The date and time stamp of the file is stored in the find_t.wr_date and find_t.wr_time structure members. The file date is stored in a two-byte unsigned integer as shown here:
Element           Offset           Range
Seconds     –     5 bits     –     0-9 (multiply by 2 to get the seconds value)
Minutes     –     6 bits     –     0-59
Hours     –     5 bits     –     0-23

Similarly, the file time is stored in a two-byte unsigned integer, as shown here:
Element           Offset           Range
Day     –     5 bits     –     1-31
Month     –     4 bits     –     1-12
Year     –     7 bits     –     0-127 (add the value “1980” to get the year value)

The following example program shows how you can get a directory listing along with each file’s date and time stamp:

#include <stdio.h>

#include <direct.h>

#include <dos.h>

#include <malloc.h>

#include <memory.h>

#include <string.h>

typedef struct find_t FILE_BLOCK;

void main(void);

void main(void)

{

FILE_BLOCK f_block;   /* Define the find_t structure variable */

int ret_code;         /* Define a variable to store return codes */

int hour;             /* We’re going to use a 12-hour clock! */

char* am_pm;          /* Used to print “am” or “pm” */

printf(“\nDirectory listing of all files in this directory:\n\n”);

/* Use the “*.*” file mask and the 0xFF attribute mask to list

all files in the directory, including system files, hidden

files, and subdirectory names. */

ret_code = _dos_findfirst(“*.*”, 0xFF, &f_block);

/* The _dos_findfirst() function returns a 0 when it is successful

and has found a valid filename in the directory. */

while (ret_code == 0)

{

/* Convert from a 24-hour format to a 12-hour format. */

hour = (f_block.wr_time >> 11);

if (hour > 12)

{

hour  = hour – 12;

am_pm = “pm”;

}

else

am_pm = “am”;

/* Print the file’s name, date stamp, and time stamp. */

printf(“%-12s  %02d/%02d/%4d  %02d:%02d:%02d %s\n”,

f_block.name,                      /* name  */

(f_block.wr_date >> 5) & 0x0F,     /* month */

(f_block.wr_date) & 0x1F,          /* day   */

(f_block.wr_date >> 9) + 1980,     /* year  */

hour,                              /* hour  */

(f_block.wr_time >> 5) & 0x3F,     /* minute  */

(f_block.wr_time & 0x1F) * 2,      /* seconds */

am_pm);

/* Use the _dos_findnext() function to look

for the next file in the directory. */

ret_code = _dos_findnext(&f_block);

}

printf(“\nEnd of directory listing.\n”);

}

N

/* This is the find_t structure as defined by ANSI C. */

struct find_t

{

char reserved[21];

char attrib;

unsigned wr_time;

unsigned wr_date;

long size;

char name[13];

}

/* This is a custom find_t structure where we

separate out the bits used for date and time. */

struct my_find_t

{

char reserved[21];

char attrib;

unsigned seconds:5;

unsigned minutes:6;

unsigned hours:5;

unsigned day:5;

unsigned month:4;

unsigned year:7;

long size;

char name[13];

}

/* Now, create a union between these two structures

so that we can more easily access the elements of

wr_date and wr_time. */

union file_info

{

struct find_t ft;

struct my_find_t mft;

}

Using the preceding technique, instead of using bit-shifting and bit-manipulating, you can now extract date and time elements like this:

file_info my_file;

printf(“%-12s  %02d/%02d/%4d  %02d:%02d:%02d %s\n”,

my_file.mft.name,             /* name    */

my_file.mft.month,            /* month   */

my_file.mft.day,              /* day     */

(my_file.mft.year + 1980),    /* year    */

my_file.mft.hours,            /* hour    */

my_file.mft.minutes,          /* minute  */

(my_file.mft.seconds * 2),    /* seconds */

am_pm);

10. How do you sort filenames in a directory?

When you are sorting the filenames in a directory, the one-at-a-time approach does not work. You need some way to store the filenames and then sort them when all filenames have been obtained. This task can be accomplished by creating an array of pointers to find_t structures for each filename that is found. As each filename is found in the directory, memory is allocated to hold the find_t entry for that file. When all filenames have been found, the qsort() function is used to sort the array of find_t structures by filename.

The qsort() function can be found in your compiler’s library. This function takes four parameters: a pointer to the array you are sorting, the number of elements to sort, the size of each element, and a pointer to a function that compares two elements of the array you are sorting. The comparison function is a user-defined function that you supply. It returns a value less than zero if the first element is less than the second element, greater than zero if the first element is greater than the second element, or zero if the two elements are equal. Consider the following example:

#include <stdio.h>

#include <direct.h>

#include <dos.h>

#include <malloc.h>

#include <memory.h>

#include <string.h>

typedef struct find_t FILE_BLOCK;

int  sort_files(FILE_BLOCK**, FILE_BLOCK**);

void main(void);

void main(void)

{

FILE_BLOCK f_block;       /* Define the find_t structure variable */

int ret_code;             /* Define a variable to store the return

codes */

FILE_BLOCK** file_list;   /* Used to sort the files */

int file_count;           /* Used to count the files */

int x;                    /* Counter variable */

file_count = -1;

/* Allocate room to hold up to 512 directory entries. */

file_list = (FILE_BLOCK**) malloc(sizeof(FILE_BLOCK*) * 512);

printf(“\nDirectory listing of all files in this directory:\n\n”);

/* Use the “*.*” file mask and the 0xFF attribute mask to list

all files in the directory, including system files, hidden

files, and subdirectory names. */

ret_code = _dos_findfirst(“*.*”, 0xFF, &f_block);

/* The _dos_findfirst() function returns a 0 when it is successful

and has found a valid filename in the directory. */

while (ret_code == 0 && file_count < 512)

{

/* Add this filename to the file list */

file_list[++file_count] =

(FILE_BLOCK*) malloc(sizeof(FILE_BLOCK));

*file_list[file_count] = f_block;

/* Use the _dos_findnext() function to look

for the next file in the directory. */

ret_code = _dos_findnext(&f_block);

}

/* Sort the files */

qsort(file_list, file_count, sizeof(FILE_BLOCK*), sort_files);

/* Now, iterate through the sorted array of filenames and

print each entry. */

for (x=0; x<file_count; x++)

{

printf(“%-12s\n”, file_list[x]->name);

}

printf(“\nEnd of directory listing.\n”);

}

int sort_files(FILE_BLOCK** a, FILE_BLOCK** b)

{

return (strcmp((*a)->name, (*b)->name));

}

(72)

Previous
Next

Leave a Reply