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

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

16. 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 functionhardretn() 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. */
     _harderr(error_handler);
     printf("\nEnsure that the A: drive is empty, \n");
     printf("then press any key.\n\n");
     getch();
     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");
                    break;
          case 2:   printf("FILE.TMP was not found on drive A!\n");
                    break;
          case 0:   printf("FILE.TMP was found on drive A!\n");
                    break;
          default:  printf("Unknown error occurred!\n");
                    break;
     }
}
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)
          _hardretn(100);
     /* Pause one second. */
     for (x=0; x<2000000; x++);
         /* Retry to access the drive. */
         _hardresume(_HARDERR_RETRY);
}

 

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. Theerror_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.


 

17. How can I read and write comma-delimited text?

Many of today's popular programs use comma-delimited text as a means of transferring data from one program to another, such as the exported data from a spreadsheet program that is to be imported by a database program. Comma-delimited means that all data (with the exception of numeric data) is surrounded by double quotation marks ("") followed by a comma. Numeric data appears as-is, with no surrounding double quotation marks. At the end of each line of text, the comma is omitted and a newline is used.

To read and write the text to a file, you would use the fprintf() and fscanf() standard C library functions. The following example shows how a program can write out comma-delimited text and then read it back in.

 

#include <stdio.h>
#include <string.h>
typedef struct name_str
{
     char       first_name[15];
     char       nick_name[30];
     unsigned years_known;
} NICKNAME;
NICKNAME nick_names[5];
void main(void);
void set_name(unsigned, char*, char*, unsigned);
void main(void)
{
     FILE*     name_file;
     int       x;
     NICKNAME tmp_name;
     printf("\nWriting data to NICKNAME.DAT, one moment please...\n");
     /* Initialize the data with some values... */
     set_name(0,    "Sheryl",      "Basset",      26);
     set_name(1,    "Joel",        "Elkinator",    1);
     set_name(2,    "Cliff",       "Shayface",    12);
     set_name(3,    "Lloyd",       "Lloydage",    28);
     set_name(4,    "Scott",       "Pie",          9);
     /* Open the NICKNAME.DAT file for output in text mode. */
     name_file = fopen("NICKNAME.DAT", "wt");
     /* Iterate through all the data and use the fprintf() function
        to write the data to a file. */
     for (x=0; x<5; x++)
     {
          fprintf(name_file, "\"%s\", \"%s\", %u\n",
                      nick_names[x].first_name,
                      nick_names[x].nick_name,
                      nick_names[x].years_known);
     }
     /* Close the file and reopen it for input. */
     fclose(name_file);
     printf("\nClosed NICKNAME.DAT, reopening for input...\n");
     name_file = fopen("NICKNAME.DAT", "rt");
     printf("\nContents of the file NICKNAME.DAT:\n\n");
     /* Read each line in the file using the scanf() function
        and print the file's contents. */
     while (1)
     {
          fscanf(name_file, "%s %s %u",
                     tmp_name.first_name,
                     tmp_name.nick_name,
                     &tmp_name.years_known);
          if (feof(name_file))
               break;
          printf("%-15s %-30s %u\n",
                     tmp_name.first_name,
                     tmp_name.nick_name,
                     tmp_name.years_known);
     }
     fclose(name_file);
}
void set_name(unsigned name_num, char* f_name, char* n_name, unsigned years)
{
     strcpy(nick_names[name_num].first_name, f_name);
     strcpy(nick_names[name_num].nick_name,  n_name);
     nick_names[name_num].years_known = years;
}
The document Data Files (Part - 4), 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 - 4)

,

MCQs

,

practice quizzes

,

Sample Paper

,

mock tests for examination

,

Exam

,

shortcuts and tricks

,

pdf

,

Data Files (Part - 4)

,

Data Files (Part - 4)

,

Important questions

,

Summary

,

study material

,

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

,

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

,

ppt

,

Free

,

Extra Questions

,

past year papers

,

Viva Questions

,

Semester Notes

,

Previous Year Questions with Solutions

,

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

,

video lectures

,

Objective type Questions

;