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.

fpos_t pos;<br />

char buf[25];<br />

if ((myfile = fopen("sampfgetpos.c", "rb")) ==<br />

NULL)<br />

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

else<br />

{<br />

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

if (fgetpos(myfile, &pos) != 0)<br />

perror("fgetpos error");<br />

else<br />

{<br />

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

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

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

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

}<br />

if (fsetpos(myfile, &pos) != 0)<br />

perror("fsetpos error");<br />

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

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

fclose(myfile);<br />

}<br />

}<br />

Output:<br />

Bytes read: program opens a file<br />

Bytes read: and reads bytes at<br />

Bytes read: program opens a file<br />

Standard C Libraries with Math Functions<br />

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

fgets<br />

Description: Get a string from a stream<br />

Include: <br />

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

Arguments: s pointer to the storage string<br />

n maximum number of characters to read<br />

stream pointer to the open stream.<br />

Return Value: Returns a pointer to the string sif successful; otherwise, returns a<br />

null<br />

khác<br />

printf ("afile1 đã được đóng cửa \ n");<br />

}<br />

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

printf ("Thứ hai cố gắng, không thể mở afile1 \ n");<br />

khác<br />

{<br />

printf ("Thứ hai cố gắng, afile1 được mở \ n");<br />

y = fclose (myfile1);<br />

if (y == EOF)<br />

printf ("afile1was không đóng cửa \ n");<br />

khác<br />

printf ("afile1 đã được đóng cửa \ n");<br />

}<br />

if ((myfile2 = fopen ("afile2", "w +")) == NULL)<br />

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

khác<br />

{<br />

printf ("afile2 được mở \ n");<br />

y = fclose (myfile2);<br />

if (y == EOF)<br />

printf ("afile2was không đóng cửa \ n");<br />

khác<br />

printf ("afile2 đã được đóng cửa \ n");<br />

}<br />

}<br />

Đầu ra:<br />

Không thể mở afile1<br />

Thứ hai cố gắng, afile1 đã được mở<br />

afile1 đã bị đóng cửa<br />

afile2 đã được mở<br />

afile2 đã bị đóng cửa<br />

Giải thích:<br />

afile1must tồn tại trước khi nó có thể được mở ra để đọc (r) hoặc<br />

fopenfunction sẽ thất bại. Nếu fopenfunction mở một tập tin để viết<br />

(W +) nó không phải đã tồn tại. Nếu nó không tồn tại, nó sẽ được tạo ra<br />

và sau đó mở ra.<br />

fopen (Tiếp theo)<br />

dsPIC

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

Saved successfully!

Ooh no, something went wrong!