- ทุกครั้งทำการอ่านหรือเขียนไฟล์ พอร์ยเตอร์จะชี้ไปที่ตำแหน่งล่าสุดที่ไฟล์นั้นถูกใช้งาน
rewind( pt );
// ทำการเซ็ตค่าพอร์ยเตอร์ไปที่ตำแหน่งเริ่มต้นของไฟล์long ftell(FILE *p);
// บอกตำแหน่งปัจจุบันของไฟล์พอร์ยเตอร์
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define BUFFER 10 | |
int main(void) | |
{ | |
setbuf(stdout,NULL); | |
char *message = "Hello banyentutorial.blogspot.com"; | |
char *fileName = "coolTutorial.txt"; | |
FILE *p; | |
if((p = fopen(fileName,"w")) == NULL) exit(-1); | |
if (fputs(message,p) == EOF ) exit(-1); | |
fclose(p); | |
if((p = fopen(fileName,"r")) == NULL) exit(-1); | |
char buf[BUFFER]; | |
fgets(buf,BUFFER,p); | |
printf("%s | %d\n" , buf ,ftell(p)); | |
fgets(buf,BUFFER,p); | |
printf("%s | %d\n" , buf ,ftell(p)); | |
fgets(buf,BUFFER,p); | |
printf("%s | %d\n" , buf ,ftell(p)); | |
rewind(p); | |
fgets(buf,BUFFER,p); | |
printf("%s | %d\n" , buf ,ftell(p)); | |
return 0; | |
} |