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.

if (y)<br />

perror("Fseek failed");<br />

else<br />

{<br />

fgets(s, 70, myfile);<br />

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

}<br />

fclose(myfile);<br />

}<br />

}<br />

Output:<br />

"This is the beginning"<br />

"this is the middle andthis is the end."<br />

"this is the end."<br />

Explanation:<br />

The file afile.outis created with the text, "This is the beginning, this<br />

is the middle and this is the end".<br />

The function fseekuses an offset of zero and SEEK_SETto set the file<br />

pointer to the beginning of the file. fgetsthen reads 22 characters<br />

which are "This is the beginning, " and adds a null character to the<br />

string.<br />

Next, fseekuses an offset of two and SEEK_CURRENTto set the file<br />

pointer to the current position plus two (skipping the comma and<br />

space.) fgetsthen reads up to the next 70characters. The first 39<br />

characters are "this is the middle and this is the end." It stops when it<br />

reads EOF and adds a null character to the string.<br />

FInally, fseekuses an offset of negative 16 characters and SEEK_END<br />

to set the file pointer to 16 characters from the end of the file. fgets<br />

then reads up to 70 characters. It stops at the EOF after reading 16<br />

characters "this is the end." and adds a null character to the string.<br />

fsetpos<br />

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

Include: <br />

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

Arguments: streamtarget stream<br />

pos position-indicator storage as returned by an earlier call to<br />

fgetpos<br />

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

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

}<br />

}<br />

Đầu ra:<br />

Đọc một số ký tự:<br />

"Câu isa này rất dài"<br />

Vị trí hiện tại của con trỏ tập tin là 29<br />

dsPIC<br />

®<br />

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

DS51456B trang 248<br />

fwrite<br />

Mô tả: Ghi dữ liệu vào dòng.<br />

Bao gồm:<br />

2004 Microchip Technology Inc<br />

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

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

Đối số: ptr con trỏ đến bộ đệm lưu trữ<br />

kích thước kích thước của sản phẩm<br />

số nelem tối đa các mặt hàng được đọc<br />

dòng con trỏ đến dòng mở<br />

Quay trở lại giá trị: Trả về số phần tử hoàn chỉnh văn bản thành công, mà<br />

sẽ ít hơn nelemonly nếu một lỗi ghi là gặp phải.<br />

Ghi chú: Các chức năng viết chữ vào một dòng nhất định từ một bộ đệm chỉ<br />

để bằng ptrup để nelemelements có kích thước được xác định bởi kích<br />

thước. Các<br />

chỉ báo vị trí tập tin được đưa lên bằng số lượng ký tự<br />

viết thành công. Nếu functionsets chỉ số lỗi,<br />

chỉ số tập tin vị trí là không xác định.<br />

Ví dụ: # include / * Cho fread, fwrite, * /<br />

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

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

int main (void)<br />

{<br />

FILE * buf;<br />

int x, numwrote, numread;<br />

nums đôi [10], readnums [10];<br />

if ((buf = fopen ("afile.out", "w +")) = NULL)<br />

{<br />

for (x = 0; x

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

Saved successfully!

Ooh no, something went wrong!