30.10.2014 Views

o_195h4n6al16jb186b1b2qs7fgssa.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

SEEK_CUR– seeks from the current position of the file pointer<br />

SEEK_END– seeks from the end of the file<br />

Example: #include /* for fseek, fgets, */<br />

/* printf, fopen, fclose, */<br />

/* FILE, NULL, perror, */<br />

/* SEEK_SET, SEEK_CUR, */<br />

/*SEEK_END */<br />

int main(void)<br />

{<br />

FILE *myfile;<br />

char s[70];<br />

int y;<br />

myfile = fopen("afile.out", "w+");<br />

if (myfile == NULL)<br />

printf("Cannot open afile.out\n");<br />

else<br />

{<br />

fprintf(myfile, "This is the beginning, "<br />

"this is the middle and "<br />

"this is the end.");<br />

y = fseek(myfile, 0L, SEEK_SET);<br />

if (y)<br />

perror("Fseek failed");<br />

else<br />

{<br />

fgets(s, 22, myfile);<br />

printf("\"%s\"\n\n", s);<br />

}<br />

y = fseek(myfile, 2L, SEEK_CUR);<br />

if (y)<br />

perror("Fseek failed");<br />

else<br />

{<br />

fgets(s, 70, myfile);<br />

printf("\"%s\"\n\n", s);<br />

}<br />

Standard C Libraries with Math Functions<br />

2004 Microchip Technology Inc. DS51456B-page 245<br />

y = fseek(myfile, -16L, SEEK_END);<br />

Byte đọc: chương trình sẽ mở ra một tập tin<br />

fsetpos (Tiếp theo)<br />

Thư viện chuẩn C với hàm toán học<br />

2004 Microchip Technology Inc DS51456B trang 247<br />

ftell<br />

Mô tả: Được vị trí hiện tại của con trỏ tập tin.<br />

Bao gồm:<br />

Prototype: dài ftell (FILE * stream);<br />

Đối số: dòng dòng, trong đó để có được vị trí tập tin hiện hành<br />

Quay trở lại giá trị: Trả về vị trí của con trỏ tập tin nếu thành công; nếu không,<br />

trả về<br />

-1.<br />

Ví dụ: # include / * Cho ftell, fread, * /<br />

/ * Fprintf, printf, * /<br />

/ * Fopen, fclose, sizeof, * /<br />

/ * FILE, NULL * /<br />

int main (void)<br />

{<br />

FILE * myfile;<br />

char s [75];<br />

y dài;<br />

myfile = fopen ("afile.out", "w +");<br />

if (myfile == NULL)<br />

printf ("Không thể mở afile.out \ n");<br />

khác<br />

{<br />

fprintf (myfile, "isa này câu rất dài"<br />

"Để đưa vào các tập tin có tên"<br />

"Afile.out để thử nghiệm.");<br />

fclose (myfile);<br />

if ((myfile = fopen ("afile.out", "rb")) = NULL)<br />

{<br />

printf ("Đọc một số ký tự: \ n");<br />

fread (s, sizeof (char), 29 tuổi, myfile);<br />

printf ("\ t \"% s \ "\ n", s);<br />

y = ftell (myfile);<br />

printf ("vị trí hiện tại của"<br />

"Con trỏ tập tin là% ld \ n", y);<br />

fclose (myfile);<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!