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.

Explanation:<br />

afile1must exist before it can be opened for reading (r) or the<br />

fopenfunction will fail. If the fopenfunction opens a file for writing<br />

(w+) it does not have to already exist. If it doesn't exist, it will be created<br />

and then opened.<br />

fopen (Continued)<br />

dsPIC<br />

®<br />

Language Tools Libraries<br />

DS51456B-page 238<br />

fprintf<br />

2004 Microchip Technology Inc.<br />

Description: Prints formatted data to a stream.<br />

Include: <br />

Prototype: int fprintf(FILE *stream, const char *format, ...);<br />

Arguments: stream pointer to the stream in which to output data<br />

format format control string<br />

... optional arguments<br />

Return Value: Returns number of characters generated or a negative number if<br />

an<br />

error occurs.<br />

Remarks: The format argument has the same syntax and use that it has in<br />

print.<br />

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

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

/* FILE, NULL */<br />

int main(void)<br />

{<br />

FILE *myfile;<br />

int y;<br />

char s[]="Printthis string";<br />

int x = 1;<br />

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

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

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

else<br />

{<br />

y = fprintf(myfile, "%s %d time%c", s, x, a);<br />

printf("Number ofcharacters printed "<br />

"to file = %d",y);<br />

{<br />

nums [x] = 10,0 / (x + 1);<br />

printf ("10,0 /% d =% f \ n", x + 1, nums [x]);<br />

}<br />

numwrote = fwrite (nums, sizeof (double),<br />

10, buf);<br />

printf ("đã viết% dnumbers \ n \ n", numwrote);<br />

fclose (buf);<br />

}<br />

khác<br />

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

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

2004 Microchip Technology Inc DS51456B trang 241<br />

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

{<br />

numread = fread (readnums, sizeof (double),<br />

10, buf);<br />

printf ("Đọc% số d \ n", numread);<br />

for (x = 0; x

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

Saved successfully!

Ooh no, something went wrong!