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

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

1. If errno contains a nonzero number, is there an error?

The global variable errno is used by many standard C library functions to pass back to your program an error code that denotes specifically which error occurred. However, your program should not check the value of errno to determine whether an error occurred.

Usually, the standard C library function you are calling returns with a return code which denotes that an error has occurred and that the value of errno has been set to a specific error number. If no error has occurred or if you are using a library function that does not reference errno, there is a good chance that errno will contain an erroneous value. For performance enhancement, the errno variable is sometimes not cleared by the functions that use it.

You should never rely on the value of errno alone; always check the return code from the function you are calling to see whether errno should be referenced. Refer to your compiler's library documentation for references to functions that utilize the errno global variable and for a list of valid values for errno.


2. What is a stream?

A stream is a continuous series of bytes that flow into or out of your program. Input and output from devices such as the mouse, keyboard, disk, screen, modem, and printer are all handled with streams. In C, all streams appear as files - not physical disk files necessarily, but rather logical files that refer to an input/output source. The C language provides five "standard" streams that are always available to your program. These streams do not have to be opened or closed. These are the five standard streams:

 

Name   Description   Example
stdin - Standard Input - Keyboard
stdout - Standard Output - Screen
stderr - Standard Error - Screen
stdprn - Standard Printer - LPT1: port
stdaux - Standard Auxiliary - COM1: port

Note that the stdprn and stdaux streams are not always defined. This is because LPT1: and COM1: have no meaning under certain operating systems. However, stdin, stdout, and stderr are always defined. Also, note that the stdin stream does not have to come from the keyboard; it can come from a disk file or some other device through what is called redirection. In the same manner, the stdout stream does not have to appear on-screen; it too can be redirected to a disk file or some other device. See the next FAQ for an explanation of redirection.

 

3. How do you redirect a standard stream?

Most operating systems, including DOS, provide a means to redirect program input and output to and from different devices. This means that rather than your program output (stdout) going to the screen, it can be redirected to a file or printer port. Similarly, your program's input (stdin) can come from a file rather than the keyboard. In DOS, this task is accomplished using the redirection characters, < and >. For example, if you wanted a program named PRINTIT.EXE to receive its input (stdin) from a file named STRINGS.TXT, you would enter the following command at the DOS prompt:

C:>PRINTIT < STRINGS.TXT

Notice that the name of the executable file always comes first. The less-than sign (<) tells DOS to take the strings contained in STRINGS.TXT and use them as input for the PRINTIT program.

Redirection of standard streams does not always have to occur at the operating system. You can redirect a standard stream from within your program by using the standard C library function named freopen(). For example, if you wanted to redirect the stdout standard stream within your program to a file named OUTPUT.TXT, you would implement the freopen() function as shown here:

 

...
freopen("output.txt", "w", stdout);
...

Now, every output statement (printf()puts()putch(), and so on) in your program will appear in the file OUTPUT.TXT.


4. How can you restore a redirected standard stream?

The preceding example showed how you can redirect a standard stream from within your program. But what if later in your program you wanted to restore the standard stream to its original state? By using the standard C library functions named dup() and fdopen(), you can restore a standard stream such as stdout to its original state.

The dup() function duplicates a file handle. You can use the dup() function to save the file handle corresponding to the stdout standard stream. The fdopen() function opens a stream that has been duplicated with the dup() function. Thus, as shown in the following example, you can redirect standard streams and restore them:

 

#include <stdio.h>
void main(void);

void main(void)
{
     int orig_stdout;
     /* Duplicate the stdout file handle and store it in orig_stdout. */
     orig_stdout = dup(fileno(stdout));
     /* This text appears on-screen. */
     printf("Writing to original stdout...\n");
     /* Reopen stdout and redirect it to the "redir.txt" file. */
     freopen("redir.txt", "w", stdout);
     /* This text appears in the "redir.txt" file. */
     printf("Writing to redirected stdout...\n");
     /* Close the redirected stdout. */
     fclose(stdout);
     /* Restore the original stdout and print to the screen again. */
     fdopen(orig_stdout, "w");
     printf("I'm back writing to the original stdout.\n");
}


5. Can stdout be forced to print somewhere other than the screen?

Although the stdout standard stream defaults to the screen, you can force it to print to another device using something called redirection. For instance, consider the following program:

 

/* redir.c */
#include <stdio.h>
void main(void);
void main(void)
{
     printf("Let's get redirected!\n");
}

 

At the DOS prompt, instead of entering just the executable name, follow it with the redirection character >, and thus redirect what normally would appear on-screen to some other device. The following example would redirect the program's output to the prn device, usually the printer attached on LPT1:

C:>REDIR > PRN

Alternatively, you might want to redirect the program's output to a file, as the following example shows:

C:>REDIR > REDIR.OUT

In this example, all output that would have normally appeared on-screen will be written to the file REDIR.OUT.


6. What is the difference between text and binary modes?

Streams can be classified into two types: text streams and binary streams. Text streams are interpreted, with a maximum length of 255 characters. With text streams, carriage return/line feed combinations are translated to the newline \n character and vice versa. Binary streams are uninterpreted and are treated one byte at a time with no translation of characters. Typically, a text stream would be used for reading and writing standard text files, printing output to the screen or printer, or receiving input from the keyboard.

A binary text stream would typically be used for reading and writing binary files such as graphics or word processing documents, reading mouse input, or reading and writing to the modem.

 

7. How do you determine whether to use a stream function or a low-level function?

Stream functions such as fread() and fwrite() are buffered and are more efficient when reading and writing text or binary data to files. You generally gain better performance by using stream functions rather than their unbuffered low-level counterparts such as read() and write().

In multiuser environments, however, when files are typically shared and portions of files are continuously being locked, read from, written to, and unlocked, the stream functions do not perform as well as the low- level functions. This is because it is hard to buffer a shared file whose contents are constantly changing.

Generally, you should always use buffered stream functions when accessing nonshared files, and you should always use the low-level functions when accessing shared files.


8. How do you list files in a directory?

Unfortunately, there is no built-in function provided in the C language such as dir_list() that would easily provide you with a list of all files in a particular directory. By utilizing some of C's built-in directory functions, however, you can write your own dir_list() function.

First of all, the include file dos.h defines a structure named find_t, which represents the structure of the DOS file entry block. This structure holds the name, time, date, size, and attributes of a file. Second, your C compiler library contains the functions _dos_findfirst() and _dos_findnext(), which can be used to find the first or next file in a directory.

The _dos_findfirst() function requires three arguments. The first argument is the file mask for the directory list. A mask of *.* would be used to list all files in the directory. The second argument is an attribute mask, defining which file attributes to search for. For instance, you might want to list only files with the Hidden or Directory attributes. The last argument of the _dos_findfirst() function is a pointer to the variable that is to hold the directory information (the find_t structure variable).

The second function you will use is the _dos_findnext() function. Its only argument is a pointer to thefind_t structure variable that you used in the _dos_findfirst() function. Using these two functions and the find_t structure, you can iterate through the directory on a disk and list each file in the directory. Here is the code to perform this task:

 

#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\n", f_block.name);
          /* 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 - 1), 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

shortcuts and tricks

,

Extra Questions

,

Important questions

,

Viva Questions

,

ppt

,

pdf

,

MCQs

,

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

,

past year papers

,

Free

,

Previous Year Questions with Solutions

,

Semester Notes

,

practice quizzes

,

Objective type Questions

,

Data Files (Part - 1)

,

Sample Paper

,

Data Files (Part - 1)

,

video lectures

,

Data Files (Part - 1)

,

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

,

Summary

,

study material

,

Exam

,

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

,

mock tests for examination

;