← Back to C Course | Chapter 10: File I/O | Lesson 5 of 6

C के fseek() और ftell()

fseek और ftell आपको एक file में किसी भी spot पर jump करने और यह ढूंढने देते हैं कि आप कहाँ हैं, एक book में एक bookmark की तरह। आपको शुरुआत से पढ़ने की ज़रूरत नहीं।
Syntax
c
fseek(file_pointer, offset, SEEK_SET);  // or SEEK_CUR, SEEK_END

long position = ftell(file_pointer);

fseek() से Random Access

SEEK_SET file की शुरुआत के relative seek करता है, SEEK_CUR current position के relative, और SEEK_END अंत के relative -- यह flexibility आपको इससे पहले सब कुछ पढ़े बिना file में कहीं भी jump करने देती है।

उदाहरण: Random access using fseek()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	FILE *fp = fopen("data.txt", "w");
	fprintf(fp, "0123456789");
	fclose(fp);
	fp = fopen("data.txt", "r");
	fseek(fp, 5, SEEK_SET);
	// Declare c and set it to fgetc(fp)
	char c = fgetc(fp);
	fclose(fp);
	// Print formatted text to the screen
	printf("%c", c);
	// Return 0 to signal that the program finished successfully
	return 0;
}

ftell() से Positions ढूंढना

एक operation से पहले और बाद में ftell() combine करना आपको exactly measure करने देता है कि एक particular read या write ने कितने bytes इस्तेमाल किए, जो custom file indexes बनाने या I/O behavior verify करने के लिए useful है।

उदाहरण: Finding positions with ftell()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	FILE *fp = fopen("data.txt", "w");
	fprintf(fp, "Hello");
	// Declare pos and set it to ftell(fp)
	long pos = ftell(fp);
	fclose(fp);
	// Print formatted text to the screen
	printf("%ld", pos);
	// Return 0 to signal that the program finished successfully
	return 0;
}

rewind() से Streams Rewind करना

यह functionally fseek(fp, 0, SEEK_SET) के identical है लेकिन ज़्यादा concise और self-documenting है जब आपको बस एक file को इसकी शुरुआत से दोबारा पढ़ना शुरू करना हो।

उदाहरण: Rewinding streams with rewind()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	FILE *fp = fopen("data.txt", "w");
	fprintf(fp, "Hello");
	fclose(fp);
	fp = fopen("data.txt", "r");
	fgetc(fp);
	rewind(fp);
	// Declare c and set it to fgetc(fp)
	char c = fgetc(fp);
	fclose(fp);
	// Print formatted text to the screen
	printf("%c", c);
	// Return 0 to signal that the program finished successfully
	return 0;
}

File Size Calculate करना

common pattern है fseek(fp, 0, SEEK_END) उसके बाद long size = ftell(fp), फिर actually data पढ़ने से पहले शुरुआत में वापस जाने के लिए fseek(fp, 0, SEEK_SET) -- यह three-step dance C file handling में एक बहुत common idiom है।

उदाहरण: Calculating file size

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	FILE *fp = fopen("data.txt", "w");
	fprintf(fp, "Hello");
	fclose(fp);
	fp = fopen("data.txt", "r");
	fseek(fp, 0, SEEK_END);
	// Declare size and set it to ftell(fp)
	long size = ftell(fp);
	fseek(fp, 0, SEEK_SET);
	fclose(fp);
	// Print formatted text to the screen
	printf("%ld", size);
	// Return 0 to signal that the program finished successfully
	return 0;
}

SEEK_END इस्तेमाल करके EOF से Move करना

SEEK_END के साथ Negative offsets पूरी file पहले पढ़े बिना इसके last N bytes पढ़ने का standard तरीका हैं, जो बहुत बड़ी files पर performance के लिए मायने रखता है।

उदाहरण: Moving from EOF using SEEK_END

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	FILE *fp = fopen("data.txt", "w");
	fprintf(fp, "0123456789");
	fclose(fp);
	fp = fopen("data.txt", "r");
	fseek(fp, -3, SEEK_END);
	// Declare c and set it to fgetc(fp)
	char c = fgetc(fp);
	fclose(fp);
	// Print formatted text to the screen
	printf("%c", c);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक text file पर ftell इस्तेमाल करना और कुछ systems पर इसके characters की संख्या के बराबर होने की उम्मीद करना।
  2. लिखने के बाद file को दोबारा पढ़ने से पहले rewind या fseek call करना भूल जाना।
  3. fseek को गलत origin constant pass करना, जैसे SEEK_SET और SEEK_END मिला देना।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.