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.

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

{<br />

printf("Read some characters:\n");<br />

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

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

y = ftell(myfile);<br />

printf("The current position of the "<br />

"file pointer is %ld\n", y);<br />

fclose(myfile);<br />

}<br />

}<br />

}<br />

Output:<br />

Read some characters:<br />

"This isa very long sentence "<br />

The current position of the file pointer is 29<br />

dsPIC<br />

®<br />

Language Tools Libraries<br />

DS51456B-page 248<br />

fwrite<br />

Description: Writes data to the stream.<br />

Include: <br />

2004 Microchip Technology Inc.<br />

Prototype: size_t fwrite(const void *ptr, size_t size,<br />

size_t nelem, FILE *stream);<br />

Arguments: ptr pointer to the storage buffer<br />

size size of item<br />

nelem maximum number of items to be read<br />

stream pointer to the open stream<br />

Return Value: Returns the number of complete elements successfully written,<br />

which<br />

will be less than nelemonly if a write error is encountered.<br />

Remarks: The function writes characters to a given stream from a buffer pointed<br />

to by ptrup to nelemelements whose size is specified by size. The<br />

file position indicator is advanced by the number of characters<br />

successfully written. If the functionsets the error indicator, the<br />

file-position indicator is indeterminate.<br />

Example: #include /* for fread, fwrite, */<br />

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

FILE * buf;<br />

char y;<br />

if ((buf = fopen ("afile.txt", "r")) == NULL)<br />

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

khác<br />

{<br />

y = getc (buf);<br />

trong khi (y! = EOF)<br />

{<br />

printf ("% c |", y);<br />

y = getc (buf);<br />

}<br />

fclose (buf);<br />

}<br />

}<br />

Đầu vào:<br />

Nội dung afile.txt (được sử dụng như đầu vào):<br />

Ngắn<br />

Chuỗi dài hơn<br />

Đầu ra:<br />

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

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

|<br />

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

2004 Microchip Technology Inc DS51456B trang 251<br />

getchar<br />

Mô tả: Có được một nhân vật từ thiết bị nhập chuẩn.<br />

Bao gồm:<br />

Nguyên mẫu: int getchar (void);<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: cùng tác dụng như fgetcwith các thiết bị nhập chuẩn đối số.<br />

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

int main (void)<br />

{<br />

char y;<br />

y = getchar ();<br />

printf ("% c |", y);<br />

y = getchar ();

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

Saved successfully!

Ooh no, something went wrong!