Files 465
Here, the contents of abc . t x t are redirected to program read • exe. The user need not enter any data
through the keyboard. The entire contents of ab c. t x t are used as input and the same is displayed.
Here, the ' <' redirection operator is used.
Input and output can be redirected simultaneously. Here, input is taken from a file and transferred to
another file. The program acts as mediator between both the files. The following command illustrates
this process.
C> READ < ABC.TXT > ALPHA•TXT
In this process, program reads redirected data from the file a b c . t x t . Also instead of displaying
output on the screen it would be redirected to the file alph a. t x t . The contents of both the files
ab c. tx t and a lp h a . t x t will be the same. User can confirm by typing them.
SU MM AR Y
This chapter explains the procedure for opening files and storing information in them. The various 1/
O functions related to high level and low level file disk operations are elaborated with numerous
examples. After having gone through structures read and write you are familiar with fw r ite () and
f read () functions. Using other file functions we describe functions related to the particular record,
end of file and detecting the errors. Command line arguments are arguments from which command
prompt of the operating system is described. Simulation of various dos command with example are
also narrated. Reader is also made familiar with I/O-REDIRECTIONS in which output of the program
can be redirected to file or printer. Also data from file can be redirected to the program which is
described.
EXERC ISES
A] Answ er the following questions.
1) What is the difference between end of a file and end of a string?
2) Distinguish between text mode and binary mode operation of a file.
3) What is the use of fseek () ? Explain its syntax.
4) Distinguish between the following functions.
a) scanf0 andfscanfO
b) getcO and getcharO
c) putcO and fputcO
d) putwO andgetwO
e) ferrorO andperrorO
f) feof() and eof()
5) How does an append mode differs from a write mode?
6) Why the header file stdio.h be frequently used in C Language?
7) Compare between printf and fprintf functions.
8) Distinguish between the following modes.
a) w and w+
b) r and r+
c) rb and rb+
d) a anda+
466 Programming and Data Structures
9) Explain low level disk operations.
10) Explain command line arguments.
11) Explain Environment Variables.
12) How re-direction of input and output is done? Explain in brief.
B) Answ er the following b y selecting the appropriate option.
1) The f scanf () statements reads data from
a) file b) keyboard
c) both (a) and (b) d) none of the above
2) When f open () fails to open a file it returns
aO NULL b) -1
c) 1 d) none of the above
3) A file opened in w+ mode can be
a) read/write b) only read
c) only write d) none of the above
4) Command line arguments are used to accept argument from
a) command prompt of operating system b) through scanf () statement
c) both (a) and (b) d) none of the above
5) The redirection operator 1 > ' transfers any output to
a) text file b) console
c) both (a) and (b) d) none of the above
6) This function is used to detect the end of file
a) fe o f() b) ferrorO c) fputsO d) fgetchO
7) The EOF is equivalent to
a) -1 b) 0
c) 1 d) none of the above
C] Attenjpt the following programs.
1) Write a program to generate a data file containing the list of cricket players, no. innings played
highest run score and no. of hatrik made by them. Use structure variable to store the cricketer's
name, no.of innings played, highest run score & number of hatricks,
2) Write a program to reposition the file to its 10th character.
3) Write a program to display contents of file on the screen. The program should ask for file name.
Display the contents in capital case.
4) Write a program to find size of the file.
5) Write a program to combine contents of two files in a third file. Add line number at the beginning
of each line.
6) Write a program to display numbers from 1 to 100. Re-direct the output of the program to text file.
7) Write a program to write contents of one file in reverse into another file.
8) Write a program to interchange contents of two files.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.237.164