Data Files - C Interview Questions and Answers

  1. How can I prevent another program from modifying part of a file that I am modifying?

    If your C compiler library comes with a function named locking() that can be used to lock and unlock portions of shared files.

    The locking function takes three arguments: a handle to the shared file you are going to lock or unlock, the operation you want to perform on the file, and the number of bytes you want to lock. The file lock is placed relative to the current position of the file pointer, so if you are going to lock bytes located anywhere but at the beginning of the file, you need to reposition the file pointer by using the lseek() function.

    The following example shows how a binary index file named SONGS.DAT can be locked and unlocked:

    #include <stdio.h>
    #include <io.h>
    #include <fcntl.h>
    #include <process.h>
    #include <string.h>
    #include <share.h>
    #include <sys\locking.h>
    void main(void);
    void main(void)
         int file_handle, ret_code;
         char* song_name = "Six Months In A Leaky Boat";
         char rec_buffer[50];
         file_handle = sopen("C:\\DATA\\SONGS.DAT", O_RDWR, SH_DENYNO);
         /* Assuming a record size of 50 bytes, position the file
            pointer to the 10th record. */
         lseek(file_handle, 450, SEEK_SET);
         /* Lock the 50-byte record. */
         ret_code = locking(file_handle, LK_LOCK, 50);
         /* Write the data and close the file. */
         memset(rec_buffer, '\0', sizeof(rec_buffer));
         sprintf(rec_buffer, "%s", song_name);
         write(file_handle, rec_buffer, sizeof(rec_buffer));
         lseek(file_handle, 450, SEEK_SET);
         locking(file_handle, LK_UNLCK, 50);

    Notice that before the record is locked, the record pointer is positioned to the 10th record (450th byte) by using the lseek() function. Also notice that to write the record to the file, the record pointer has to be repositioned to the beginning of the record before unlocking the record.

  2. How can I avoid the Abort, Retry, Fail messages?

    When DOS encounters a critical error, it issues a call to interrupt 24, the critical error handler. Your C compiler library contains a function named harderr() that takes over the handling of calls to interrupt 24. The harderr() function takes one argument, a pointer to a function that is called if there is a hardware error.

    Your user-defined hardware error-handling function is passed information regarding the specifics of the hardware error that occurred. In your function, you can display a user-defined message to avoid the ugly Abort, Retry, Fail message. This way, your program can elegantly handle such simple user errors as your not inserting the disk when prompted to do so.

    When a hardware error is encountered and your function is called, you can either call the C library function hardretn() to return control to your application or call the C library function hardresume() to return control to DOS. Typically, disk errors can be trapped and your program can continue by using the hardresume() function. Other device errors, such as a bat FAT (file allocation table) error, are somewhat fatal, and your application should handle them by using the hardretn() function. Consider the following example, which uses the harderr() function to trap for critical errors and notifies the user when such an error occurs:

    #include <stdio.h>
    #include <dos.h>
    #include <fcntl.h>
    #include <ctype.h>
    void main(void);
    void far error_handler(unsigned, unsigned, unsigned far*);
    void main(void)
         int file_handle, ret_code;
         /* Install the custom error-handling routine. */
         printf("\nEnsure that the A: drive is empty, \n");
         printf("then press any key.\n\n");
         printf("Trying to write to the A: drive...\n\n");
         /* Attempt to access an empty A: drive... */
         ret_code = _dos_open("A:FILE.TMP", O_RDONLY, &file_handle);
         /* If the A: drive was empty, the error_handler() function was
            called. Notify the user of the result of that function. */
         switch (ret_code)
              case 100: printf("Unknown device error!\n");
              case 2:   printf("FILE.TMP was not found on drive A!\n");
              case 0:   printf("FILE.TMP was found on drive A!\n");
              default:  printf("Unknown error occurred!\n");
    void far error_handler(unsigned device_error, unsigned error_val, 
                            unsigned far* device_header)
         long x;
         /* This condition will be true only if a nondisk error occurred. */
         if (device_error & 0x8000)
         /* Pause one second. */
         for (x=0; x<2000000; x++);
             /* Retry to access the drive. */

    In this example, a custom hardware error handler is installed named error_handler(). When the program attempts to access the A: drive and no disk is found there, the error_handler() function is called. The error_handler() function first checks to ensure that the problem is a disk error. If the problem is not a disk error, it returns 100 by using the hardretn() function. Next, the program pauses for one second and issues a hardresume() call to retry accessing the A: drive.