
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Explain the Random accessing files in C language
Random accessing of files in C language can be done with the help of the following functions −
- ftell ( )
- rewind ( )
- fseek ( )
ftell ( )
It returns the current position of the file ptr.
The syntax is as follows −
int n = ftell (file pointer)
For example,
FILE *fp; int n; _____ _____ _____ n = ftell (fp);
Note − ftell ( ) is used for counting the number of characters which are entered into a file.
rewind ( )
It makes file ptr move to beginning of the file.
The syntax is as follows −
rewind (file pointer);
For example,
FILE *fp; ----- ----- rewind (fp); n = ftell (fp); printf ("%d”, n);
Output
The output is as follows −
0 (always).
fseek ( )
It is to make the file pntr point to a particular location in a file.
The syntax is as follows −
fseek(file pointer, offset, position);
Offset
- The no of positions to be moved while reading or writing.
- If can be either negative (or) positive.
- Positive - forward direction.
- Negative – backward direction.
Position
It can have three values, which are as follows −
- 0 – Beginning of the file.
- 1 – Current position.
- 2 – End of the file.
Example
fseek (fp,0,2) - fp moved 0 bytes forward from the end of the file.
fseek (fp, 0, 0) – fp moved 0 bytes forward from beginning of the file
fseek (fp, m, 0) – fp moved m bytes forward from the beginning of the file.
fseek (fp, -m, 2) – fp moved m bytes backward from the end of the file.
Errors
The errors related to fseek () function are as follows −
- fseek (fp, -m, 0);
- fseek(fp, +m, 2);
- Related Articles
- Explain the concept of pointer accessing in C language
- Explain the accessing of structure variable in C language
- Explain the concept of Uninitialized array accessing in C language
- Explain the custom header files in C language
- Explain write mode operation of files in C language
- Explain read mode operation of files in C language
- Explain append mode operation of files in C language
- Explain putc() and getc() functions of files in C language
- What are the text files and binary files in C language?
- Why files are needed in C programming language?
- What are the different operations on files in C language?
- Explain C tokens in C Language
- Explain the sorting techniques in C language
- Explain the different sections in C language
- Explain the Character operations in C language
