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.

perror("fsetpos error");<br />

fread(buf, sizeof(char), 21, myfile);<br />

printf("Bytes read: %.21s\n", buf);<br />

fclose(myfile);<br />

}<br />

}<br />

Output:<br />

Bytes read: program opens a file<br />

Bytes read: and reads bytes at<br />

Bytes read: program opens a file<br />

fsetpos (Continued)<br />

Standard C Libraries with Math Functions<br />

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

ftell<br />

Description: Gets the current position of a file pointer.<br />

Include: <br />

Prototype: long ftell(FILE *stream);<br />

Argument: stream stream in which to get the current file position<br />

Return Value: Returns the position of the file pointer if successful; otherwise,<br />

returns<br />

-1.<br />

Example: #include /* for 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 />

long 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 isa very long sentence "<br />

"for input into the file named "<br />

"afile.out for testing.");<br />

fclose(myfile);<br />

1 * 10,000000 = 10,000000<br />

2 * 5,000000 = 10,000000<br />

3 * 3,333333 = 10,000000<br />

4 * 2,500000 = 10,000000<br />

5 * 2,000000 = 10,000000<br />

6 * 1,666667 = 10,000000<br />

7 * 1,428571 = 10,000000<br />

8 * 1,250000 = 10,000000<br />

9 * 1,111111 = 10,000000<br />

10 * 1,000000 = 10,000000<br />

Giải thích:<br />

Chương trình này sử dụng fwriteto tiết kiệm 10 số vào một tập tin dưới dạng nh<br />

phân.<br />

Điều này cho phép các con số để được lưu inthe cùng một khuôn mẫu của các<br />

bit là<br />

chương trình nào đang sử dụng cung cấp thêm độ chính xác và nhất quán. Sử<br />

dụng<br />

fprintfwould lưu các số như textstrings, mà có thể gây ra<br />

những con số được rút ngắn. Eachnumber được chia thành 10<br />

sản xuất một loạt các con số. Lấy các con số với một freadto<br />

mảng mới và nhân chúng bằng của số ban đầu cho thấy<br />

số không được cắt ngắn trong quá trình tiết kiệm.<br />

fwrite (Tiếp theo)<br />

dsPIC<br />

®<br />

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

DS51456B trang 250<br />

getc<br />

2004 Microchip Technology Inc<br />

Mô tả: Có được một nhân vật từ các dòng.<br />

Bao gồm:<br />

Nguyên mẫu: int getc (FILE * stream);<br />

Đối số: dòng con trỏ đến dòng mở<br />

Quay trở lại giá trị: Trả về nhân vật đọc hoặc EOF nếu một lỗi đọc xảy ra hoặc<br />

kết thúc của tập tin<br />

đạt được.<br />

Bình luận: getcis giống như chức năng fgetc.<br />

Ví dụ: # include / * Cho getc, printf, * /<br />

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

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

int main (void)<br />

{

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

Saved successfully!

Ooh no, something went wrong!