C के fseek() और ftell()
In this page:
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()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
ftell() से Positions ढूंढना
एक operation से पहले और बाद में ftell() combine करना आपको exactly measure करने देता है कि एक particular read या write ने कितने bytes इस्तेमाल किए, जो custom file indexes बनाने या I/O behavior verify करने के लिए useful है।
उदाहरण: Finding positions with ftell()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
rewind() से Streams Rewind करना
यह functionally fseek(fp, 0, SEEK_SET) के identical है लेकिन ज़्यादा concise और self-documenting है जब आपको बस एक file को इसकी शुरुआत से दोबारा पढ़ना शुरू करना हो।
उदाहरण: Rewinding streams with rewind()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
SEEK_END इस्तेमाल करके EOF से Move करना
SEEK_END के साथ Negative offsets पूरी file पहले पढ़े बिना इसके last N bytes पढ़ने का standard तरीका हैं, जो बहुत बड़ी files पर performance के लिए मायने रखता है।
उदाहरण: Moving from EOF using SEEK_END
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक text file पर
ftellइस्तेमाल करना और कुछ systems पर इसके characters की संख्या के बराबर होने की उम्मीद करना। - लिखने के बाद file को दोबारा पढ़ने से पहले
rewindयाfseekcall करना भूल जाना। fseekको गलत origin constant pass करना, जैसेSEEK_SETऔरSEEK_ENDमिला देना।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: