20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

476 Appendix B The Standard C Library<br />

associated with filePtr. If the freopen call is successful, filePtr is returned; otherwise,<br />

a null po<strong>in</strong>ter is returned.The freopen function is frequently used to reassign<br />

std<strong>in</strong>, stdout, or stderr <strong>in</strong> the program. For example, the call<br />

if ( freopen ("<strong>in</strong>putData", "r", std<strong>in</strong>) == NULL ) {<br />

...<br />

}<br />

has the effect of reassign<strong>in</strong>g std<strong>in</strong> to the file <strong>in</strong>putData, which is opened <strong>in</strong> read<br />

access mode. Subsequent I/O operations performed with std<strong>in</strong> are performed with<br />

the file <strong>in</strong>putData, as if std<strong>in</strong> had been redirected to this file when the program was<br />

executed.<br />

<strong>in</strong>t fscanf (filePtr, format, arg1, arg2, ..., argn)<br />

Reads data items from the file identified by filePtr, accord<strong>in</strong>g to the format specified<br />

by the character str<strong>in</strong>g format.The values that are read are stored <strong>in</strong> the arguments<br />

specified after format, each of which must be a po<strong>in</strong>ter.The format characters<br />

that are allowed <strong>in</strong> format are the same as those for the scanf function (see Chapter<br />

16).The fscanf function returns the number of items successfully read and assigned<br />

(exclud<strong>in</strong>g any %n assignments) or the value EOF if the end of file is reached before<br />

the first item is converted.<br />

<strong>in</strong>t fseek (filePtr, offset, mode)<br />

Positions the <strong>in</strong>dicated file to a po<strong>in</strong>t that is offset (a long <strong>in</strong>t) bytes from the<br />

beg<strong>in</strong>n<strong>in</strong>g of the file, from the current position <strong>in</strong> the file, or from the end of the file,<br />

depend<strong>in</strong>g upon the value of mode (an <strong>in</strong>teger). If mode equals SEEK_SET, position<strong>in</strong>g<br />

is relative to the beg<strong>in</strong>n<strong>in</strong>g of the file. If mode equals SEEK_CUR, position<strong>in</strong>g is relative<br />

to the current position <strong>in</strong> the file. If mode equals SEEK_END, position<strong>in</strong>g is relative to<br />

the end of the file. SEEK_SET, SEEK_CUR,and SEEK_END are def<strong>in</strong>ed <strong>in</strong> .<br />

On systems that dist<strong>in</strong>guish between text and b<strong>in</strong>ary files, SEEK_END might not be<br />

supported for b<strong>in</strong>ary files. For text files, either offset must be zero or must be a<br />

value returned from a prior call to ftell. In the latter case, mode must be SEEK_SET.<br />

If the fseek call is unsuccessful, a nonzero value is returned.<br />

<strong>in</strong>t fsetpos (filePtr, fpos)<br />

Sets the current file position for the file associated with filePtr to the value po<strong>in</strong>ted<br />

to by fpos, which is of type fpos_t (def<strong>in</strong>ed <strong>in</strong> ). Returns zero on success,<br />

and nonzero on failure. See also fgetpos.<br />

long ftell (filePtr)<br />

Returns the relative offset <strong>in</strong> bytes of the current position <strong>in</strong> the file identified by<br />

filePtr, or –1L on error.

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

Saved successfully!

Ooh no, something went wrong!