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.

Description: Opens a file.<br />

Include: <br />

Prototype: FILE *fopen(const char *filename, const char *mode);<br />

Arguments: filename name of the file<br />

mode type of access permitted<br />

Return Value: Returns a pointer to the open stream. If the function fails a null<br />

pointer<br />

is returned.<br />

Remarks: Following are the types of file access:<br />

r- opens an existing text file for reading<br />

w- opens an empty text file for writing. (An existing file will<br />

be overwritten.)<br />

a- opens a text file for appending. (A file is created if it<br />

doesn't exist.)<br />

rb- opens an existing binary file for reading.<br />

wb- opens an empty binary file for writing. (An existing file<br />

will be overwritten.)<br />

ab- opens a binary file for appending. (A file is created if it<br />

doesn't exist.)<br />

r+- opens an existing text file for reading and writing.<br />

w+- opens an empty text file for reading and writing. (An<br />

existing file will be overwritten.)<br />

a+- opens a text file for reading and appending. (A file is<br />

created if it doesn't exist.)<br />

r+bor rb+- opens an existing binary file for reading and writing.<br />

w+bor wb+- opens an empty binary file for reading and writing. (An<br />

existing file will be overwritten.)<br />

a+bor ab+- opens a binary file for reading and appending. (A file is<br />

created if it doesn't exist.)<br />

Example: #include /* for fopen, fclose, */<br />

/* printf, FILE, */<br />

/* NULL, EOF */<br />

int main(void)<br />

{<br />

FILE *myfile1, *myfile2;<br />

int y;<br />

Standard C Libraries with Math Functions<br />

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

if ((myfile1 = fopen("afile1", "r")) == NULL)<br />

2004 Microchip Technology Inc DS51456B trang 239<br />

fputc<br />

Mô tả: Đặt một ký tự dòng.<br />

Bao gồm:<br />

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

Đối số: ký tự c được viết<br />

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

Quay trở lại giá trị: Trả về nhân vật được viết orEOF nếu không gặp lỗi ghi.<br />

Ghi chú: Các chức năng ghi các ký tự dòng đầu ra, tiến bộ<br />

chỉ số tập tin vị trí và trả về ký tự như một unsigned char<br />

chuyển đổi sang một int.<br />

Ví dụ: # include / * Cho fputc, kết thúc tập tin, thiết bị xuất chuẩn * /<br />

int main (void)<br />

{<br />

char * y;<br />

char buf [] = "Đây là văn bản \ n";<br />

int x;<br />

x = 0;<br />

cho (y = buf; (x = EOF) && (* y = '\ 0');! y ++!)<br />

{<br />

x = fputc (* y, thiết bị xuất chuẩn);<br />

fputc ('|', thiết bị xuất chuẩn);<br />

}<br />

}<br />

Đầu ra:<br />

T | h | i | s | | i | s | | t | e | x | t |<br />

|<br />

fputs<br />

Mô tả: Đặt một chuỗi các dòng.<br />

Bao gồm:<br />

Nguyên mẫu: int fputs (const char * s, FILE * stream);<br />

Đối số: chuỗi s được viết<br />

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

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

về EOF.<br />

Chú ý: chức năng viết ký tự tothe sản lượng dòng lên đến nhưng không<br />

bao gồm các ký tự null.<br />

Ví dụ: # include / * Cho fputs, thiết bị xuất chuẩn * /<br />

int main (void)<br />

{

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

Saved successfully!

Ooh no, something went wrong!