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.

*mode, FILE *stream);<br />

Arguments: filename name of the new file<br />

mode type of access permitted<br />

stream pointer to the currently open stream<br />

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

pointer<br />

is returned.<br />

Remarks: The function closes the file associated with the stream as though<br />

fclosewas called. Then it opens the new file as though fopenwas<br />

called. freopenwill fail if the specified stream is not open. See fopen<br />

for the possible types of file access.<br />

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

/* printf, fclose, */<br />

/* FILE, NULL */<br />

int main(void)<br />

{<br />

FILE *myfile1, *myfile2;<br />

int y;<br />

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

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

else<br />

{<br />

printf("afile1 was opened\n");<br />

if ((myfile2 = freopen("afile2", "w+",<br />

myfile1)) == NULL)<br />

{<br />

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

fclose(myfile1);<br />

}<br />

else<br />

{<br />

printf("afile2 was opened\n");<br />

fclose(myfile2);<br />

}<br />

}<br />

}<br />

Output:<br />

afile1 was opened<br />

afile2 was opened<br />

FILE * myfile;<br />

char s [70];<br />

int y;<br />

myfile = fopen ("afile.out", "w +");<br />

if (myfile == NULL)<br />

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

khác<br />

{<br />

fprintf (myfile, "Đây là sự khởi đầu"<br />

"Đây là trung bình và"<br />

"Đây là kết thúc.");<br />

y = fseek (myfile, 0l, SEEK_SET);<br />

if (y)<br />

perror ("fseek thất bại");<br />

khác<br />

{<br />

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

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

}<br />

y = fseek (myfile, 2L, SEEK_CUR);<br />

if (y)<br />

perror ("fseek thất bại");<br />

khác<br />

{<br />

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

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

}<br />

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

2004 Microchip Technology Inc DS51456B trang 245<br />

y = fseek (myfile, -16L, SEEK_END);<br />

if (y)<br />

perror ("fseek thất bại");<br />

khác<br />

{<br />

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

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

}<br />

fclose (myfile);<br />

}

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

Saved successfully!

Ooh no, something went wrong!