Interview Preparation Exam  >  Interview Preparation Notes  >  Placement Papers - Technical & HR Questions  >  Data Files (Part - 2), C Programming Interview Questions

Data Files (Part - 2), C Programming Interview Questions | Placement Papers - Technical & HR Questions - Interview Preparation PDF Download

9. 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)

Because DOS stores a file's seconds in two-second intervals, only the values 0 to 29 are needed. You simply multiply the value by 2 to get the file's true seconds value. Also, because DOS came into existence in 1980, no files can have a time stamp prior to that year. Therefore, you must add the value "1980" to get the file's true 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  d/d/M  d:d:d %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");
}

 

Notice that a lot of bit-shifting and bit-manipulating had to be done to get the elements of the time variable and the elements of the date variable. If you happen to suffer from bitshiftophobia (fear of shifting bits), you can optionally code the preceding example by forming a union between the find_t structure and your own user-defined structure, such as this:

 

/* 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  d/d/M  d:d:d %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));
}

 

This example uses the user-defined function named sort_files() to compare two filenames and return the appropriate value based on the return value from the standard C library function strcmp(). Using this same technique, you can easily modify the program to sort by date, time, extension, or size by changing the element on which the sort_files() function operates.

 

11. How do you determine a file's attributes?

The file attributes are stored in the find_t.attrib structure member. This structure member is a single character, and each file attribute is represented by a single bit. Here is a list of the valid DOS file attributes:

 

Value   Description   Constant
0x00 - Normal - (none)
0x01 - Read Only - FA_RDONLY
0x02 - Hidden File - FA_HIDDEN
0x04 - System File - FA_SYSTEM
0x08 - Volume Label - FA_LABEL
0x10 - Subdirectory - FA_DIREC
0x20 - Archive File - FA_ARCHIVE

To determine the file's attributes, you check which bits are turned on and map them corresponding to the preceding table. For example, a read-only hidden system file will have the first, second, and third bits turned on. A "normal" file will have none of the bits turned on. To determine whether a particular bit is turned on, you do a bit-wise AND with the bit's constant representation.

The following program uses this technique to print a file's attributes:

 

#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 the return codes */
     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)
     {
          /* Print the file's name */
          printf("%-12s  ", f_block.name);
          /* Print the read-only attribute */
          printf("%s ", (f_block.attrib & FA_RDONLY) ? "R" : ".");
          /* Print the hidden attribute */
          printf("%s ", (f_block.attrib & FA_HIDDEN) ? "H" : ".");
          /* Print the system attribute */
          printf("%s ", (f_block.attrib & FA_SYSTEM) ? "S" : ".");
          /* Print the directory attribute */
          printf("%s ", (f_block.attrib & FA_DIREC)  ? "D" : ".");
          /* Print the archive attribute */
          printf("%s\n", (f_block.attrib & FA_ARCH)  ? "A" : ".");
          /* 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");
}

 

The document Data Files (Part - 2), C Programming Interview Questions | Placement Papers - Technical & HR Questions - Interview Preparation is a part of the Interview Preparation Course Placement Papers - Technical & HR Questions.
All you need of Interview Preparation at this link: Interview Preparation
85 docs|57 tests

Top Courses for Interview Preparation

85 docs|57 tests
Download as PDF
Explore Courses for Interview Preparation exam

Top Courses for Interview Preparation

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Data Files (Part - 2)

,

Sample Paper

,

Previous Year Questions with Solutions

,

video lectures

,

practice quizzes

,

shortcuts and tricks

,

Extra Questions

,

study material

,

C Programming Interview Questions | Placement Papers - Technical & HR Questions - Interview Preparation

,

Free

,

mock tests for examination

,

Viva Questions

,

Objective type Questions

,

Summary

,

pdf

,

C Programming Interview Questions | Placement Papers - Technical & HR Questions - Interview Preparation

,

ppt

,

Exam

,

Data Files (Part - 2)

,

Semester Notes

,

Data Files (Part - 2)

,

past year papers

,

Important questions

,

C Programming Interview Questions | Placement Papers - Technical & HR Questions - Interview Preparation

,

MCQs

;