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.

Explanation:<br />

This program uses myfile2to point to the stream when freopenis<br />

called so if an error occurs, myfile1will still point to the stream and<br />

can be closed properly. If the freopencall is successful, myfile2can<br />

be used to close the stream properly.<br />

fscanf<br />

Description: Scans formatted text from a stream.<br />

Include: <br />

Standard C Libraries with Math Functions<br />

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

Prototype: int fscanf(FILE *stream, const char *format, ...);<br />

Arguments: stream pointer to the open stream from which to read data<br />

format format control string<br />

... optional arguments<br />

Return Value: Returns the number of items successfully converted and assigned.<br />

If<br />

no items are assigned, a 0 is returned. EOF is returned if end of file is<br />

encountered before the first conversion or if an error occurs.<br />

Remarks: The format argument has the same syntax and use that it has in<br />

scanf.<br />

Example: #include /* for fopen, fscanf, */<br />

/* fclose, fprintf, */<br />

/* fseek, printf, FILE, */<br />

/* NULL, SEEK_SET */<br />

int main(void)<br />

{<br />

FILE *myfile;<br />

char s[30];<br />

int x;<br />

char a;<br />

if ((myfile = fopen("afile", "w+")) == NULL)<br />

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

else<br />

{<br />

fprintf(myfile, "%s %d times%c",<br />

"Print this string", 100, '\n');<br />

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

fscanf(myfile, "%s", s);<br />

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

}<br />

Đầu ra:<br />

"Đây là sự khởi đầu"<br />

"Đây là andthis giữa là kết thúc."<br />

"Đây là kết thúc."<br />

Giải thích:<br />

Các tập tin afile.outis tạo ra với các văn bản, "Đây là sự khởi đầu, điều này<br />

là giữa và đây là kết thúc ".<br />

Chức năng fseekuses một bù đắp bằng không và SEEK_SETto thiết lập các tập<br />

tin<br />

con trỏ đến đầu của tập tin. fgetsthen đọc 22 ký tự<br />

mà là "Đây là sự khởi đầu", và cho biết thêm một ký tự null đến<br />

chuỗi.<br />

Tiếp theo, fseekuses một bù đắp của hai SEEK_CURRENTto và thiết lập các<br />

tập tin<br />

con trỏ đến vị trí hiện tại cộng với hai (bỏ qua các dấu phẩy và<br />

không gian.) fgetsthen đọc đến 70characters tiếp theo. Đầu tiên 39<br />

nhân vật "này là giữa và đây là kết thúc." Nó dừng lại khi nó<br />

đọc kết thúc tập tin và cho biết thêm một ký tự null vào chuỗi.<br />

Cuối cùng, fseekuses bù đắp âm 16 ký tự và SEEK_END<br />

thiết lập các tập tin con trỏ đến 16 ký tự từ cuối của tập tin. fgets<br />

sau đó đọc lên đến 70 ký tự. Nó dừng lại ở kết thúc tập tin sau khi đọc 16<br />

nhân vật "này là kết thúc." và cho biết thêm một ký tự null vào chuỗi.<br />

fsetpos<br />

Mô tả: Thiết lập vị trí tập tin của dòng.<br />

Bao gồm:<br />

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

Đối số: dòng streamtarget<br />

pos lưu trữ vị trí, chỉ số như trả về bởi một cuộc gọi trước đó để<br />

fgetpos<br />

Quay trở lại giá trị: Trả về 0 nếu thành công; nếu không, trả về một giá trị khác<br />

không.<br />

Chú ý: chức năng đặt chỉ số tập tin vị trí cho các dòng được đưa ra trong * pos<br />

nếu thành công; nếu không, fsetpossets errno.<br />

fseek (Tiếp theo)<br />

dsPIC<br />

®<br />

Thư viện Công cụ Ngôn ngữ<br />

DS51456B trang 246<br />

2004 Microchip Technology Inc<br />

Ví dụ: / * Chương trình này sẽ mở ra một tập tin và đọc byte tại * /<br />

/ * Địa điểm khác nhau. Các fgetpos * /

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

Saved successfully!

Ooh no, something went wrong!