30.10.2014 Views

o_195h4n6al16jb186b1b2qs7fgssa.pdf

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

fclose(buf);<br />

}<br />

}<br />

Input:<br />

Contents of afile.txt(used as input):<br />

Short<br />

Longer string<br />

Output:<br />

S|h|o|r|t|<br />

|L|o|n|g|e|r| |s|t|r|i|n|g|<br />

|<br />

dsPIC<br />

®<br />

Language Tools Libraries<br />

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

fgetpos<br />

Description: Gets the stream's file position.<br />

Include: <br />

Prototype: int fgetpos(FILE *stream, fpos_t *pos);<br />

Arguments: stream target stream<br />

pos position-indicator storage<br />

Return Value: Returns 0 if successful; otherwise, returns a non-zero value.<br />

Remarks: The function stores the file-position indicator for the given stream in<br />

*posif successful, otherwise, fgetpossets errno.<br />

Example: /* This program opens a file and reads bytes at */<br />

/* several different locations. The fgetpos */<br />

/* function notes the 8th byte. 21 bytes are */<br />

/* read then 18 bytes are read. Next the */<br />

/* fsetpos function is set based on the */<br />

/* fgetpos position and theprevious 21 bytes */<br />

/* are reread. */<br />

#include /* for fgetpos, fread, */<br />

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

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

/* fpos_t, sizeof */<br />

int main(void)<br />

{<br />

FILE *myfile;<br />

được trả về.<br />

Bình luận: Sau đây là các loại hình truy cập file:<br />

r- mở một tập tin văn bản hiện hành để đọc<br />

w- mở một tập tin văn bản trống cho văn bản. (Một tập tin hiện có sẽ<br />

được ghi đè.)<br />

a-mở một tập tin văn bản để phụ thêm. (Một tập tin được tạo ra nếu nó<br />

không tồn tại.)<br />

rb- mở một tập tin nhị phân hiện có để đọc.<br />

wb- mở một tập tin nhị phân có sản phẩm nào cho văn bản. (Một tập tin hiện có<br />

sẽ được ghi đè.)<br />

ab- mở một tập tin nhị phân để phụ thêm. (Một tập tin được tạo ra nếu nó<br />

không tồn tại.)<br />

r + - mở một tập tin văn bản hiện hành để đọc và viết.<br />

w + - mở một tập tin văn bản trống để đọc và viết. (An<br />

tập tin hiện tại sẽ được ghi đè.)<br />

a + - mở một tập tin văn bản để đọc và phụ thêm. (Một tập tin là<br />

tạo ra nếu nó không tồn tại.)<br />

r + bor rb + - mở một tập tin nhị phân hiện có để đọc và viết.<br />

w + bor wb + - mở một tập tin nhị phân có sản phẩm nào để đọc và viết. (An<br />

tập tin hiện tại sẽ được ghi đè.)<br />

a + ab + bor - mở một tập tin nhị phân để đọc và phụ thêm. (Một tập tin là<br />

tạo ra nếu nó không tồn tại.)<br />

Ví dụ: # include / * Cho fopen, fclose, * /<br />

/ * Printf, FILE, * /<br />

/ * NULL, kết thúc tập tin * /<br />

int main (void)<br />

{<br />

FILE * myfile1, * myfile2;<br />

int y;<br />

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

2004 Microchip Technology Inc DS51456B trang 237<br />

if ((myfile1 = fopen ("afile1", "r")) == NULL)<br />

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

khác<br />

{<br />

printf ("afile1 được mở \ n");<br />

y = fclose (myfile1);<br />

if (y == EOF)<br />

printf ("afile1was không đóng cửa \ n");

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

Saved successfully!

Ooh no, something went wrong!