C Programming - Data Files
Why should I learn to solve C Programming: Data Files technical interview questions?
Learn and practise solving C Programming: Data Files technical interview questions and answers to enhance your skills for clearing technical interviews, HR interviews, campus interviews, and placement tests.
Where can I get technical C Programming: Data Files technical interview questions and answers with explanations?
IndiaBIX provides you with lots of fully solved C Programming: Data Files technical interview questions and answers with a short answer description. You can download C Programming: Data Files technical interview questions and answers as PDF files or e-books.
How do I answer C Programming: Data Files technical interview questions from various companies?
You can answer all kinds of C Programming: Data Files technical interview questions by practising the given exercises (short answer type). You can also find the frequently asked C Programming: Data Files technical interview questions with answers from various companies, such as TCS, Wipro, Infosys, CTS, IBM, etc.
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.
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.
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.
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");
}