File pointer positioning in C

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

FunctionUse
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 pointer
  • offset → कितने bytes move करना है
  • position → reference point

Position Values

ConstantMeaning
SEEK_SETbeginning (start)
SEEK_CURcurrent position
SEEK_ENDend 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

  1. file open की गई
  2. fseek(fp, 6, SEEK_SET);
    • pointer को शुरुआत से 6 bytes आगे ले गया
  3. 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

  1. ftell() initial position (0) दिखाता है
  2. fgetc() से एक character पढ़ा
  3. फिर 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

  1. दो characters read किए गए
  2. pointer position 2 हो गई
  3. rewind(fp) → pointer start (0) पर वापस गया

Output:

Position before rewind: 2
Position after rewind: 0

Summary Table

FunctionWork
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/backward
  • ftell() → current time position
  • rewind() → 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 बनता है।

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top