Computer Programming C: UNIT V: File Operations

The ftell(), fseek(), rewind() Functions

C Programming

We have seen the files that are useful for reading and writing data. When we are interested in accessing only a particular part of a file and not in reading the other parts. This can be achieved with the help of the following functions.

THE ftell(), fseek(), rewind() FUNCTIONS

We have seen the files that are useful for reading and writing data. When we are interested in accessing only a particular part of a file and not in reading the other parts. This can be achieved with the help of the following functions.

i) ftell()

ii) fseek()

iii) rewind()


i) ftell(): This function is used to specify the current position of a file. It takes the file pointer and returns a number of type long, that corresponds to the current position.

Syntax: n = ftell (fp);

Description: n is the relative offset of the current position in bytes.


ii) fseek(): It is used to move the file position to a desired location within the file.

Syntax: fseek(file ptr, offset, position);

Description:

file ptr is a pointer in the file concerned.

offset is a number or variable of type long.

position is an integer number. It specifies the number of positions to be moved from the location specified by position.

These functions are very useful to handle the Random Access data files.


iii) rewind(): It is used to takes a file pointer to the starting position of the file and resets the position to the start of the file.

Example:

rewind (fp);

ftell(fp);

Where, 0 is assigned to n because the file position has been set to the start of the file by rewind.

This function is used to read a file more than once, without closing and opening the file.

NOTE: Remember that whenever a file is opened for reading or writing, a rewind is done implicitly.

Example: Program using ftell(), fseek() and rewind() functions.

/* Program using ftell(), fseek() and rewind() functions */

#include <stdio.h>

main()

{

/* Local definitions */

long int x = 0, m;

/* Statements */

FILE *fptr = fopen("vrb.dat", "r");

if (fptr != NULL)

{

        fseek(fptr, 0L, 0); /* fseek() is a function used to position desired point */

        while(x = getc(fptr) != EOF)  /* check while x=getc(fptr) is not equalled to the end of file */

        putchar(c);

        fseek(fptr, 0L, 2);

        x = ftell (fptr); /*ftell() used to specify the current position*/

        printf("\n The size of the file is %d", x);

        rewind(fptr); /*rewind to move control to the beginning */

        x = ftell (fptr);

        printf("\n The size of the file is %d", x);

} /* if */

else

        printf("\n The file does not exists");

fclose(fptr);  /* file close function closes a file associated with file pointer */

} /* main */

OUTPUT

vrb publishers pvt ltd.

The size of the file is 23

The size of the file is 0.

EXPLANATION: This program stores some text into the file vrb.dat, and using ftell, fseek(), and rewind() functions it displays the length of the file in concern with the file pointer.

 

Computer Programming C: UNIT V: File Operations : Tag: Computer Science : C Programming - The ftell(), fseek(), rewind() Functions


Computer Programming C: UNIT V: File Operations



Under Subject


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation