EXRCISE Fillin the Blanks
1 is a collection of data.
2. The standard streams in C are
3. Select the
(a) stdin
standard
stream in C.
(b) stdout
(d) all
of
does
these
a C
prOgram
(c) stderr stream
, and which
standard
4. From
3 are pre-connected input and output read data? (b) stdout
channels between a text terminal and the
these
(a) stdin
(d) allof and
program. stream
mode if it is (c) stderr between
4. Afile must be opened in an
interface
being openedfor updating. 5. What acts as
function can be used to move the file
hardware? (b) buffer
5.
marker at the beginning of the file. (a) file pointer (d) stdin
with a
6. If a file is opened in 'wb' mode then it is a (c) stdout associate a file
file opened for writing. function is used to
Which
6.
7. Block input in binary file is done using the stream? (b) fopen ()
function. (a) fread () (d) fflush ()
8. is the stream where a program writes (c) floes () character
from
its output data. returns the next there
Which
function reached, or if
7. is
9 is a block of memory that is used for stream, EOF if
the end of file
temporary storage of data. is an error?
buffer is (b) fgets ()
10. The creation and operation of the (a) fgetc ()
automatically handled by the (d) fwrite ()
(c) fputc ()
11. file can be processed sequentially as
well as randomly. State True or False
12. function closes the file and flushes all
without opening it.
the buffers that are maintained for that file. 1. You can use a file before exitig
close all the files
13. To use fprintf () to write on the screen specify 2. It is mandatory to
in place of the file pointer. the program.
updating and stream in C.
14. If you have opened a stream for 3. stderr is a standard
writing or you try to position the
later youwant to switch from reading to
or
4. An error will be generated if
vice versa, you must first use the file marker beyond EOF.
opened in "r
function.
EOF is defined in
5. A file can be read if and only if it is
15. The symbolic constant mode.
and has the value
6. Binaryfiles are slower than text files.
7. In text files, the datais stored in internal format of
Multiple Choice Questions
the computer.
1. Which function gives the current position of the 8. Text files can store only character data.
file?
9. Standard output is an output stream used by
(a) fseek () (b) fsetpos () programs to report error messages or diagnostics.
(c) ftell () (d) Rewind ()
10. Each line in a text file can have maximum of 80
2. Which function is used to perform block output in characters.
binary iles? 11. Binary file stores data in a human readable format.
(a) fwrite () (b) fprintf () 12. fread () returnsthe number of elementssuccessfully
(c) fputc () (d) fputs () read.
EXRCISE
|376| Programming in C
13. fseek () is used to
14. The tmpfile () opens
reposition
the
With access parameters set as
a binary stream.
corresponding
"W+.
fgetpos () can
be used
stream
23.
24.
Why is it not
to the
Write a
possible
same file
short
performing operations.
25. Give the
to read from a
without resetting the
nnote
importance of rewind ()
on error
fle and
file write
handling pointwhier?le
15. The value obtained by using between rewind () and
only with the fsetpos (). 26. Differentiate
alternative to (|).fseek Can
fseek () work as an rewind ()? If
Review Questions how? yes,
Write a program to write employees
Then read thedetails in a
1. What is afile? 27.
file called employee.dat.
2. Why do we need to store data in files? the nh employee and calculate his salary record of
3. Define the term stream. the nth record
binary hle. 28. Write a prOgram to read and
4. Differentiate between a text file and a
can be it on screen. Repeat the procedure until dis-1playis
S. Explain the different modes in which a file entered.
opened in a C program.
fail? 29. Write a program to copy a ile using feof ()
6. Under which circumstances does the fopen ()
7. Why should you close a file after it is used? 30. Create a file and store some names in it. Write a
data? program read the names in the file in the
O. What is the impact of fclose () on buffered order without re-opening the file.
reverse
9. Differentiate between gets () and fgets ().
10. What is the difference between a buffered output 31. Why do we need fflush (0?
stream andan unbuffered output stream? 32. Write a prOgram to read the contents of a binary
11. With the help of a program code, explain the file.
significance of ftell (). 33. Write a program to read the contents of atet
12. Under what circumstances does the ftell ()fail? file.
13. Where can ftell () be used? 34. Wriite a program tO write Some contents into lal a
14. With the help of a program, explain the significance binary file (b)a text file.
of command line arguments. 35. Write aprogram to read a text file using fscanf ).
15. Write ashort note on fgetpos () and fsetpos (). Give 36. Write aprogram to read a text file using fgetc ().
aprogram to illustrate their usage in a Cprogram. 37. Write a program to illustrate the use of fprintf().
16. What will happen if the argument to remove ) 38. Write a program to illustrate the use of fputc ().
specifies adirectory?Also state its behaviour when 39. Differentiate between fscanf () and fread ().
the argument is a filename.
17. Give a program that uses a temporary file created 40. Write a program to count the number of characters
using the tmpfile (). in a file.
18. Give the importance of associating abuffer with 41. Write a program that reads the file name and text
the stream. of 20 words as command line arguments. Write
19. Write a short note on functions that are used
to:
the text into a file whose name is given as the file
(a) read data from a file (b) write data to a file. name.
20. Write a short note on the following functions. 42. Assume that a file INTEGERS.TXT stores only integer
For each function, give a program code numbers. Avalue '-1' is stored as the last value to
that
demonstrates itS usage. indicate EOF. Write a program to read each integer
(a) fopen 0 (b) fclose () value stored in the file. While reading the value,
(c) ferror () (d) clearerr () Compute whether the value is even or odd. If it is
21. What do you understand by EOF? even then write that value in a file called EVEN.TXT
else write it in ODD.TXT. Finally display tthe contents
22, How will you check for EOF when
reading a file? of the two files- EVEN.TXT and ODD.TXI.
Hint: You may use
are same as getc getw and putw functions. They while ((C=getchar ) ) !=EOF)
()
getw() and putw() areand putc (). The prototypes of putc (c, fp); C h a r a C t e r s
entered
int getw(FILE *fp); print f('\n No. of
43. Write a and putw(int value, = $ld", ftel1 (fp) );
program to read data from FILE "fp);
fclose (fp);
and write it to a the keyboard
file. Read the
the file and display
it on the
contents stored in
44. Write a screen. 2. main()
menu-driven
append, delete, and edit program
a
to read, insert,
file. record stored in a binary FILE * fp;
char Coment (20];
45. Write a program to
read data
store it in a binary file. Also from a text file and
int i;
ip-fopen ("Feedbacks","w"):
the binary file and read the data stored in
display it on the screen. for (i = 0;i < 5;i++)
46. Write a program to
read a text file, convert all
lower case characters into the fscanf (stdin, "%s",
corrment);
upper case and re-write
the upper case characters in printf (fp, "%s",
comment);
the file. Before the
end of the program, all
delete. temporary files must be
fclose (fp);
47. Modify the above code
fragment to allow the
user three chances to enter a valid
filename. If a 3. main()
valid file name is not entered after
three chances,
terminate the program. char C;
48. Assume that there are two FILE *fp;
files- Names1 and
Names2 that stores sorted names of students who fp = fopen ("temp", " w+b"))
would be participating in Activity1 and Activity2, for (c = 'A' ;c <= ' I ' , C++)
respectively. Create a file NAMES.TXT which stores fputc (c, fp);
the names from both the files. Note that there fseek (fp, 2, 0);
should be no repetition of names in NAMES.TXT c = fgetc (fp);
and while writing name into it, ensure that the file printf ("%c", c);
is also sorted. fclose (fp);
49. Write a program to create a file that stores only
integer values. Append the sum of these integers 4. main()
at the end of the file.
50. Write aprogram that reads a binaryfile that stores char Ci
employees records and prints, on the screen, the FILE *fp;
number of records that are stored in the file. ip = fopen ("temp","w+b"))
51. Write a program to append a binary file at the end for (c = 'A' ;c <= I', C++)
of another. fwrite (&C, 1, 1, fpi
rewind (fp);
Program output fread (&C,1,1,fp);
printf ("%c", c);
Give the output of the following code. fclose (fp);
1. main()
5. main()
FILE *fp;
char C;
char c;
ip=fopen ("abe" , "w");
EX RCISE
378| Programming in C
FILE*fp;
long int pos;
tp = fopen ( "temp ", "w+b") )
for (c = 'A' ;c <= 'I', C++)
fwrite (&i, sizeof (int) . 1,
fseek (fp, sizeof (int) *2, SEEK
fread(&i, sizeof (int),1, fp):
printf (" %d", i);
SET);
£p);
fwrite (&c, 1, 1, fp); fclose (fp);
pos = ftell (fp);
pos = -3;
7. main()
fseek (fp, pos, SEEK END);
fread (&c, 1, 1,fp); int i;
printf ("$c", c); FILE tfp;
fclose (fp); long int pos;
fp = fopen ("temp", "w+b))
6. main() for (i = l;i <= l0;i++)
fwrite (&i, sizeof (int), 1, fp);
int i; fseek (fp, -sizeof (int) *2, SEEK CUR):
FILE *fp; fread (&i,sizeof (int),1,fp);
long int pOS ; print f ("$d" , i);
fp = fopen ("temp", "w+b")) fclose (fp);
for (i = 1;i <= 10;i++)