C Language Data Files Interview Questions and Answers For Freshers Part-2
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 */
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
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.
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.
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.