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.

pointer<br />

Remarks: The function reads characters from the input stream and stores them<br />

into the string pointed to by suntil it has read n-1 characters, stores a<br />

newline character or sets the end-of-file or error indicators. If any<br />

characters were stored, a null character is stored immediately after the<br />

last read character in the next element of the array. If fgetssets the<br />

error indicator, the array contents are indeterminate.<br />

Example: #include /* for fgets, printf, */<br />

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

/* FILE, NULL */<br />

#define MAX 50<br />

int main(void)<br />

{<br />

FILE *buf;<br />

char s[MAX];<br />

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

printf("Cannot open afile.txt\n");<br />

else<br />

{<br />

while (fgets(s, MAX, buf) != NULL)<br />

{<br />

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

}<br />

fclose(buf);<br />

}<br />

}<br />

Input:<br />

Contents of afile.txt(used as input):<br />

Short<br />

Longer string<br />

Output:<br />

Short<br />

|Longer string<br />

|<br />

dsPIC<br />

®<br />

Language Tools Libraries<br />

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

fopen<br />

®<br />

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

DS51456B trang 238 2004 Microchip Technology Inc<br />

fprintf<br />

Mô tả: In định dạng dữ liệu vào một dòng.<br />

Bao gồm:<br />

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

Đối số: dòng con trỏ vào dòng, trong đó để dữ liệu đầu ra<br />

định dạng kiểm soát chuỗi định dạng<br />

Đối số tùy chọn ...<br />

Quay trở lại giá trị: Trả về số ký tự được tạo ra hoặc một số âm nếu một<br />

lỗi xảy ra.<br />

Ghi chú: Đối số định dạng có cú pháp tương tự và sử dụng nó có trong<br />

in.<br />

Ví dụ: # include / * Cho fopen, fclose, * /<br />

/ * Fprintf, printf, * /<br />

/ * FILE, NULL * /<br />

int main (void)<br />

{<br />

FILE * myfile;<br />

int y;<br />

char s [] = "chuỗi Printthis";<br />

int x = 1;<br />

char '\ n' a =;<br />

if ((myfile = fopen ("afile", "w")) == NULL)<br />

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

khác<br />

{<br />

y = fprintf (myfile, "% s% d% thời gian c", s, x, a);<br />

printf ("ofcharacters Số in"<br />

"Nộp =% d", y);<br />

fclose (myfile);<br />

}<br />

}<br />

Đầu ra:<br />

Số ký tự được in để nộp = 25<br />

Nội dung afile:<br />

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

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

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

Saved successfully!

Ooh no, something went wrong!