Data Files - C Interview Questions and Answers

  1. 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
    0x01-Read Only-FA_RDONLY
    0x02-Hidden File-FA_HIDDEN
    0x04-System File-FA_SYSTEM
    0x08-Volume Label-FA_LABEL
    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  ",;
              /* 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");

  2. How do you view the PATH?

    Your C compiler library contains a function called getenv() that can retrieve any specified environment variable. It has one argument, which is a pointer to a string containing the environment variable you want to retrieve. It returns a pointer to the desired environment string on successful completion. If the function cannot find your environment variable, it returns NULL.

    The following example program shows how to obtain the PATH environment variable and print it on-screen:

    #include <stdio.h>
    #include <stdlib.h>
    void main(void);
    void main(void)
         char* env_string;
         env_string = getenv("PATH");
         if (env_string == (char*) NULL)
              printf("\nYou have no PATH!\n");
              printf("\nYour PATH is: %s\n", env_string);