Professional Documents
Culture Documents
4.
5.
FILES
A file is a place on disk where group of related data are stored. C supports a
number of functions that have the ability to perform basic file operations, which
include:
Naming a file
Opening the file
Reading data from the file
Writing data to the file
Closing the file
There are two distinct ways to perform file operations in C. The first one is
known as the low level I/O and uses UNIX system calls. The second method is
referred to as the high level I/O operation and uses functions in C's standard I/O
library.
FILE TYPES:
DOS treats files in two different ways viz., as binary or text files. Almost all
UNIX Systems do not make any distinction between the two. If a file is specified as
the binary type, the file I/O functions do not interpret the contents of the file when
they read from or write to the file. But, if the file is specified as the text type the I/O
functions interpret the contents of the file. The basic differences in these two types of
files are:
I. ASCII 0xla is considered an end-of-file character by the file I/O function
when reading from a text file and they assume that the end of the file has
been reached.
II. In case of DOS a new file is stored as the sequence 0*0d 0*0a on the disk
in case of text files. UNIX stores \n as 0* 0a both on disk and in memory.
FILE Operations:
Department of CSE, GIT, GU
Page 158
The first statement declares the variable fp as a pointer to the data type FILE. As
stated earlier, File is a structure that is defined in the I/O Library. The second
statement opens the file named filename and assigns an identifier to the FILE type
pointer fp. This pointer, which contains all the information about the file, is
subsequently used as a communication link between the system and the program.
The second statement also specifies the purpose of opening the file. The mode does
this job.
r open the file for read only.
w open the file for writing only.
a open the file for appending data to it.
r+ open an existing file for update (reading and writing)
w+ create a new file for update. If it already exists, it will be overwritten.
a+ open for append; open for update at the end of the file
Consider the following statements:
FILE *pl, *p2;
pI =fopen("data" ,"r");
p2=fopen("results", "w");
In these statements the pI and p2 are created and assigned to open the files data
and results respectively the file data is opened for reading and result is opened for
writing. In case the results file already exists, its contents are deleted and the files are
Department of CSE, GIT, GU
Page 159
fclose(p1); fclose(p2)
The above program opens two files and closes them after all operations on
them are
completed, once a file is closed its file pointer can be reversed on other
file.
Page 160
4. Reading a integer from a file and writing a integer into a file ( getw() and
putw() functions)
These are integer-oriented functions. They are similar to get c and putc
functions and are used to read and write integer values. These functions would be
useful when we deal with only integer data. The general forms of getw and putw are:
putw(integer,fp );
getw(fp);
Page 161
Page 162
Page 163
Page 164
Page 165
Page 166
Page 167
Page 168
Page 169
Page 170
d) 0
3. Which of the following function is used to set the record pointer at the beginning of
the file
[c]
a) Putc()
b) getw()
c) rewind() d) gets()
4. Which of the following mode opeqs a binary file in write mode [d]
a) W+
b)Y wb+
c) w
d) wb
5. Which of the following functions used to write an entire block of data to a file [d]
a) Fputs
b). fgets
c) fscanf
d) fwrite
6. The fscanf() function reads data from [a]
a) File
b) keyboard c) both a and b d) none
7. When fopen() fails to open the file it returns [a]
a) Null
b) -1
c) 1
d) none
Page 171
d) none
c) feof(fp,list)
d) none
Page 172
Page 173