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.

Argument: stream pointer to FILEstructure<br />

Return Value: Returns a nonzero value iferror indicator is set; otherwise, returns<br />

a<br />

zero.<br />

Example: /* This program tries to write to a file that is */<br />

/* readonly. This causes the error indicator to */<br />

/* be set. The functionferror is used to check */<br />

/* the error indicator and find the error. The */<br />

/* function clearerr is used to reset the error */<br />

/* indicator so the next time ferror is called */<br />

/* it will not report an error. */<br />

#include /* for ferror, clearerr, */<br />

/* printf, fprintf, */<br />

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

/* FILE, NULL */<br />

int main(void)<br />

{<br />

FILE *myfile;<br />

if ((myfile = fopen("sampclearerr.c", "r")) ==<br />

NULL)<br />

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

else<br />

{<br />

fprintf(myfile, "Write this line to the "<br />

"file.\n");<br />

if (ferror(myfile))<br />

printf("Error\n");<br />

else<br />

printf("No error\n");<br />

clearerr(myfile);<br />

if (ferror(myfile))<br />

printf("Still has Error\n");<br />

else<br />

printf("Error indicator reset\n");<br />

fclose(myfile);<br />

}<br />

}<br />

Output:<br />

Error<br />

{<br />

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

if (fgetpos (myfile, và pos) = 0)<br />

perror ("fgetpos lỗi");<br />

khác<br />

{<br />

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

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

fread (buf, sizeof (char), 18 tuổi, myfile);<br />

printf ("Bytes đọc:% .18s \ n", buf);<br />

}<br />

if (fsetpos (myfile, và pos) = 0)<br />

perror ("fsetpos lỗi");<br />

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

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

fclose (myfile);<br />

}<br />

}<br />

Đầu ra:<br />

Byte đọc: chương trình sẽ mở ra một tập tin<br />

Byte đọc: và đọc byte tại<br />

Byte đọc: chương trình sẽ mở ra một tập tin<br />

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

2004 Microchip Technology Inc DS51456B trang 235<br />

fgets<br />

Mô tả: Có được một chuỗi từ một dòng suối<br />

Bao gồm:<br />

Prototype: char * fgets (char * s, int n, FILE * stream);<br />

Đối số: s con trỏ đến chuỗi lưu trữ<br />

n số lượng tối đa của các nhân vật để đọc<br />

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

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

về một null<br />

con trỏ<br />

Bình luận: Chức năng đọc ký tự từ dòng đầu vào và lưu trữ chúng<br />

vào chuỗi trỏ đến bởi suntil nó đã đọc n-1 ký tự, lưu trữ một<br />

xuống dòng nhân vật hoặc thiết lập cuối cùng của tập tin hoặc các chỉ số<br />

lỗi. Nếu bất kỳ<br />

ký tự được lưu trữ, một ký tự null được lưu trữ ngay lập tức sau khi<br />

cuối cùng đọc ký tự trong phần tử tiếp theo của mảng. Nếu các fgetssets

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

Saved successfully!

Ooh no, something went wrong!