Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Explain the Random accessing files in C language
Random access in C allows you to move the file pointer to any position within a file, enabling non-sequential reading and writing operations. This is accomplished using three key functions that control file pointer positioning.
Syntax
long ftell(FILE *stream); void rewind(FILE *stream); int fseek(FILE *stream, long offset, int whence);
ftell() Function
The ftell() function returns the current position of the file pointer as a long integer representing the byte offset from the beginning of the file.
#include <stdio.h>
int main() {
FILE *fp;
long position;
fp = fopen("test.txt", "w");
if (fp == NULL) {
printf("Error opening file
");
return 1;
}
fprintf(fp, "Hello World");
position = ftell(fp);
printf("Current position: %ld
", position);
fclose(fp);
return 0;
}
Current position: 11
rewind() Function
The rewind() function moves the file pointer to the beginning of the file, equivalent to calling fseek(fp, 0, SEEK_SET).
#include <stdio.h>
int main() {
FILE *fp;
long position;
fp = fopen("test.txt", "w+");
if (fp == NULL) {
printf("Error opening file
");
return 1;
}
fprintf(fp, "Hello World");
printf("Position after writing: %ld
", ftell(fp));
rewind(fp);
position = ftell(fp);
printf("Position after rewind: %ld
", position);
fclose(fp);
return 0;
}
Position after writing: 11 Position after rewind: 0
fseek() Function
The fseek() function moves the file pointer to a specific location in the file based on an offset and a reference position.
Parameters
- offset − Number of bytes to move (can be positive or negative)
-
whence − Reference position:
-
SEEK_SET(0) − Beginning of file -
SEEK_CUR(1) − Current position -
SEEK_END(2) − End of file
-
#include <stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("test.txt", "w+");
if (fp == NULL) {
printf("Error opening file
");
return 1;
}
fprintf(fp, "ABCDEFGHIJ");
/* Move to 5th position from beginning */
fseek(fp, 5, SEEK_SET);
ch = fgetc(fp);
printf("Character at position 5: %c
", ch);
/* Move 2 positions backward from current */
fseek(fp, -2, SEEK_CUR);
ch = fgetc(fp);
printf("Character after moving back 2: %c
", ch);
/* Move to 3rd position from end */
fseek(fp, -3, SEEK_END);
ch = fgetc(fp);
printf("3rd character from end: %c
", ch);
fclose(fp);
return 0;
}
Character at position 5: F Character after moving back 2: E 3rd character from end: H
Common Usage Examples
-
fseek(fp, 0, SEEK_END)− Move to end of file -
fseek(fp, 0, SEEK_SET)− Move to beginning of file -
fseek(fp, 10, SEEK_SET)− Move 10 bytes from beginning -
fseek(fp, -5, SEEK_END)− Move 5 bytes back from end
Key Points
-
fseek()returns 0 on success, non-zero on failure - Random access works best with binary files
- Text files may have platform-specific line ending issues
- Always check return values for error handling
Conclusion
Random file access in C provides powerful control over file operations through ftell(), rewind(), and fseek() functions. These functions enable efficient data manipulation by allowing direct positioning within files without sequential reading.
