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.

10.0/2 = 5.000000<br />

10.0/3 = 3.333333<br />

10.0/4 = 2.500000<br />

10.0/5 = 2.000000<br />

10.0/6 = 1.666667<br />

10.0/7 = 1.428571<br />

10.0/8 = 1.250000<br />

10.0/9 = 1.111111<br />

10.0/10 = 1.000000<br />

Wrote 10 numbers<br />

Read 10 numbers<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 />

Explanation:<br />

This program uses fwriteto save 10 numbers to a file in binary form.<br />

This allows the numbers to be saved inthe same pattern of bits as the<br />

program is using which provides more accuracy and consistency. Using<br />

fprintfwould save the numbers as text strings which could cause<br />

the numbers to be truncated. Eachnumber is divided into 10 to<br />

produce a variety of numbers. Retrieving the numbers with freadto a<br />

new array and multiplying them by the original number shows the<br />

numbers were not truncated in the save process.<br />

fread (Continued)<br />

dsPIC<br />

®<br />

Language Tools Libraries<br />

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

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

fclose (myfile);<br />

}<br />

}<br />

Đầu vào:<br />

Nội dung afile:<br />

In chuỗi này 100 lần<br />

Đầu ra:<br />

In<br />

này<br />

chuỗi<br />

100<br />

lần<br />

fscanf (Tiếp theo)<br />

dsPIC<br />

®<br />

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

DS51456B trang 244<br />

fseek<br />

2004 Microchip Technology Inc<br />

Mô tả: Di chuyển con trỏ tập tin đến một địa điểm cụ thể.<br />

Bao gồm:<br />

Nguyên mẫu: int fseek (FILE * stream, dài bù đắp, int mode);<br />

Đối số: dòng dòng, trong đó để di chuyển con trỏ tập tin.<br />

giá trị bù đắp thêm vào vị trí hiện tại<br />

loại phương thức tìm kiếm để thực hiện<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 và thiết lập<br />

errno.<br />

Bình luận: chế độ có thể là một trong các cách sau:<br />

SEEK_SET- tìm kiếm từ đầu của tập tin<br />

SEEK_CUR- tìm từ vị trí hiện tại của con trỏ tập tin<br />

SEEK_END- tìm kiếm từ khi kết thúc tập tin<br />

Ví dụ: # include / * Cho fseek, fgets, * /<br />

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

DS51456B-page 242<br />

freopen<br />

2004 Microchip Technology Inc.<br />

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

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

Description: Reassigns an existing stream to a new file.<br />

Include: <br />

Prototype: FILE *freopen(const char *filename, const char<br />

/ * SEEK_END * /<br />

int main (void)<br />

{

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

Saved successfully!

Ooh no, something went wrong!