How to 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:
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”);
}
No comments:
Post a Comment