Introduction
C language में file handling करते समय file pointer की position बहुत important होती है।
File pointer यह बताता है कि file में अभी किस position (byte/character) पर काम हो रहा है।
कभी-कभी हमें:
- file के बीच से data पढ़ना होता है
- specific location पर data लिखना होता है
- pointer को start या end पर ले जाना होता है
इन सभी कामों के लिए file pointer positioning functions का उपयोग किया जाता है।
File Pointer Positioning Functions
| Function | Use |
|---|---|
fseek() | pointer को किसी position पर ले जाना |
ftell() | current position बताना |
rewind() | pointer को शुरुआत (start) पर लाना |
1. fseek() Function
Definition
fseek() का उपयोग file pointer को किसी specific position पर move करने के लिए किया जाता है।
Syntax
fseek(file_pointer, offset, position);
Parameters
file_pointer→ file pointeroffset→ कितने bytes move करना हैposition→ reference point
Position Values
| Constant | Meaning |
|---|---|
SEEK_SET | beginning (start) |
SEEK_CUR | current position |
SEEK_END | end of file |
Example Program:
#include <stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("file.txt", "r");
if (fp == NULL) {
printf("File not found\n");
return 1;
}
fseek(fp, 6, SEEK_SET);
ch = fgetc(fp);
printf("Character at position 6: %c\n", ch);
fclose(fp);
return 0;
}
Program Explanation
- file open की गई
fseek(fp, 6, SEEK_SET);- pointer को शुरुआत से 6 bytes आगे ले गया
fgetc()से character read किया
Direct position access किया गया (random access)
Output:
Character at position 6: W
2. ftell() Function
Definition
ftell() file pointer की current position (byte number) return करता है।
Syntax
position = ftell(file_pointer);
Example Program:
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("file.txt", "r");
if (fp == NULL) {
printf("File not found\n");
return 1;
}
printf("Current position: %ld\n", ftell(fp));
fgetc(fp);
printf("Position after reading one character: %ld\n", ftell(fp));
fclose(fp);
return 0;
}
Program Explanation
ftell()initial position (0) दिखाता हैfgetc()से एक character पढ़ा- फिर
ftell()position update (1) दिखाता है
Output:
Current position: 0
Position after reading one character: 1
3. rewind() Function
Definition
rewind() file pointer को वापस start (beginning) पर ले जाता है।
Syntax
rewind(file_pointer);
Example Program:
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("file.txt", "r");
if (fp == NULL) {
printf("File not found\n");
return 1;
}
fgetc(fp);
fgetc(fp);
printf("Position before rewind: %ld\n", ftell(fp));
rewind(fp);
printf("Position after rewind: %ld\n", ftell(fp));
fclose(fp);
return 0;
}
Program Explanation
- दो characters read किए गए
- pointer position 2 हो गई
rewind(fp)→ pointer start (0) पर वापस गया
Output:
Position before rewind: 2
Position after rewind: 0
Summary Table
| Function | Work |
|---|---|
fseek() | pointer move करना |
ftell() | position बताना |
rewind() | pointer start पर लाना |
Important Points
- File pointer byte position पर काम करता है
fseek()random access के लिए important हैftell()debugging और tracking में useful हैrewind()start पर reset करता है
Real Life Example
Video player में:
fseek()→ video forward/backwardftell()→ current time positionrewind()→ video restart
Exam / Practical Tips
fseek()syntax बहुत important हैSEEK_SET,SEEK_CUR,SEEK_ENDयाद रखें- positioning program अक्सर पूछा जाता है
Conclusion
File pointer positioning C file handling का advanced और powerful concept है।
यह हमें file के किसी भी हिस्से को directly access करने की सुविधा देता है, जिससे program efficient और flexible बनता है।