Sei sulla pagina 1di 19

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name :M.Subramanyam
Designation :Senior Lecturer
Branch :Computer Engg.
Institute :Q.Q.Govt.Polytechnic,Hyderabad.
Year/Semester :III Semester
Subject :UNIX & C
Subject Code :CM-304
Topic :Files
Duration :50 Min
Sub Topic :Random access files
Teaching Aids :PPTs, Animations

CM304.89 1
Recap

 The file I/O functions which are discussed so far


deal with reading and writing data sequentially.

 Direct access is not possible.

CM304.89 2
Objectives

On completion of this period, you would be able


to know…

 read, write or update file randomly.


 Use Random Access file functions.

CM304.89 3
Random Access files

 Random access files


 Access individual records without searching
through other records.
 Instant access to records in a file.
 Data can be inserted without destroying other
data.
 Data previously stored can be updated or
deleted.
CM304.89 4
Functions for Random accessing a file

C provides the following functions

 fseek()

 rewind()

 ftell()

CM304.89 5
fseek() function

 Places a file pointer at desired location.


 Sets file position pointer to a specific position.
fseek ( FILE pointer, offset,mode/symbolic_constant);
 FILE pointer – pointer to file.
 offset – file position pointer (0 is first location).
 symbolic_constant – specifies where in file we are
reading from.

CM304.89 6
fseek() function
Contd..

Symbolic_
mode Meaning
constant
Move the file pointer always
0 SEEK_SET from the beginning of the file.
Move the file pointer from the
1 SEEK_CUR current position.
Move the file pointer always
2 SEEK_END from the end of the file.

CM304.89 7
fseek() function
Contd..

fseek() call Action


fseek(fp,0L,0) Go to begining
fseek(fp,0L,1) Stay at current position

fseek(fp,0L,2) Go to end of the file

fseek(fp,m,1) Move forward by m bytes

Go backward by m bytes from the


fseek(fp,-m,1)
current position

CM304.89 8
Example

CM304.89 9
rewind() function

 rewind():
 Resets the file pointer at beginning of the file.
 Syntax:
rewind(FILE *);
Example : rewind(fp)
fp-file pointer

CM304.89 10
ftell() Function

 Returns no of bytes from the beginning of the file


upto the current position of the file pointer.
 Normally used to find the size of the file.
Syntax:
long ftell(fp);
fp-file pointer
Example
 long r;
 FILE *fp;
 r=ftell(fp);
CM304.89 11
Example1

CM304.89 12
Example2

CM304.89 13
Summary
In this class, you have learnt about …
 The file can be accessed randomly.
 The fseek() is used to move the file pointer to desired
location.

 The file pointer can be moved to beginning of the


using rewind().

 ftell() gives current position of the file pointer.

CM304.89 14
Quiz

1)The function call fseek(fp,0L,0) is same as


a) fp=fopen()
b) rewind(fp)
c) fclose(fp)
c) ftell(fp)

CM304.89 15
Quiz

1)The function call fseek(fp,0L,0) is same as


a) fp=fopen()
b) rewind(fp)
c) fclose(fp)
c) ftell(fp)

CM304.89 16
Quiz

2)ftell()

a) is function

b) gives current file position

c) can be used find the size of the file

d) all the above

CM304.89 17
Quiz

2)ftell()

a) is function

b) gives current file position

c) can be used find the size of the file

d) all the above

CM304.89 18
Frequently Asked Questions

1. What are the different random access

functions in files?

2. Explain fseek() function with an example?

CM304.89 19