C Language File Operation function (Basic article)

Source: Internet
Author: User

void Clearerr (FILE *stream):

Resets the error token and the EOF pointer of the given stream

int feof (FILE *stream):

Returns a non-0 value when reaching the end of a file stream

int ferror (FILE *stream):

If an error occurs, no zero is returned, otherwise a non-0 value is returned

void perror (const char *STR):

Print str and corresponding execution error to errno (global variable)

int fclose (FILE *stream):

Closes the given file stream, releases the buffer that was associated to the stream, executes a successful return zero, or returns EOF

int fgetc (FILE *stream):

Returns the next character from the stream, reaching the end of the file, or an error returning EOF (FPUTC returns also the character and EOF)

Getc PUTC fgetc FPUTC equivalent (but getc and PUTC are said to be more efficient and I can't understand it now)

int Fgetpos (FILE *stream, fpos_t *position):

(fpos_t defined in stdio.h) holds the position pointer of the given file stream to the given variable position, successfully returning a value of 0, failing to return a non-0 value

int Fsetpos (FILE *stream, const fpos_t *position):

Moves the pointer to position, returns a value of 0 successfully, fails to return a value other than 0

Long Ftell (FILE *stream):

Returns the current file position of the stream, error 1

Char *fgets (char *str, int num, FILE *stream):

Reads [num-1] characters from a file stream and dumps them into Str

1.fgets () stops at the end of the line and STR will be terminated by a new line character

2.fgets () reaches [num-1] characters or encounters eof,str with null end

Successfully returned STR, failed to return null

fscanf : The return value is the number of variables that are actually assigned, and if no allocations are made, EOF is returned

fprintf : The return value is the number of characters in the output, and the error returns negative (-1?). Try it!! )

File *freopen (const char *fname, const char *mode, FILE *stream):

Freopen is used to redistribute an existing stream to a different file and mode (mode) after calling this function, the given file stream will be referenced in mode mode fname, successfully return the stream, failed to return null

int fseek (FILE *stream, long offset, int origin):

A successful return of zero, fail returns nonzero, you can use Fseek () to move more than one file (!! ), but you cannot use fseek to clear the EOF tag associated to the stream before you begin

int fread (void *buffer, size_t size, size_t num, FILE *stream):

function fread () reads [NUM] Objects (each size is sized), replaces to buffer, returns the amount of content read

int fwrite (const void *buffer, size num, FILE *stream):

The return value is the number of objects that have been written

Char *gets (char *str):

The Get function (note differs from fgets) reads characters from stdin and loads them into STR until a new line is encountered \ n or reaches EOF, and the new line character is translated to a null delimiter

The return value is a read-in string and the error returns null

Int puts (char *str):

Writes string to stdout successful return non-negative, failed to return EOF

int remove (const char *fname):

Delete the file specified by fname, successfully returned 0, error returned nonzero

int rename (const char *oldfname, const char *newfname):

Successful return 0, error returned non-zero

void Rewine (FILE *stream):

Move to the beginning of the stream, clearing both the error and the EOF tag associated with the stream

int sscanf (const char *buffer, const char *format, ...); Read from buffer

int sprintf (const *buffer, const char *format, ...); The output is sent to buffer

=============================================================================

The following excerpt from the network:

=============================================================================

1. Common usage.

The following is a reference fragment:
   Charstr [512]={0};
   sscanf ("123456", "% s ", str );
   printf (" str =% s ", str );

2. Take a string of the specified length. As in the following example, take a string with a maximum length of 4 bytes.

The following is a reference fragment:
   sscanf ("123456", "%4s", str );
   printf (" str =% s ", str );

3. The string to take to the specified character. As in the following example, the string is encountered until the space is met.

The following is a reference fragment:
   sscanf ("123456abcdedf", "%[^]", str );
   printf (" str =% s ", str );

4. Take a string that contains only the specified character set. As in the following example, take a string that contains only 1 to 9 and lowercase letters.

The following is a reference fragment:
   sscanf ("123456abcdedfBCDEF", "%[1 -9a- z ]", str );
   printf (" str =% s ", str );

5. The string to be taken to the specified character set. As in the following example, take a string that encounters an uppercase letter.

The following is a reference fragment:
   sscanf ("123456abcdedfBCDEF", "%[^ A- z ]", str );
   printf (" str =% s ", str );

in addition, format can not only delimit strings with spaces, but can also be defined with other characters, enabling simple string segmentation (more flexible string splitting using strtok () ). For example:
sscanf ("2006:03:18", "% d:% d:% D ", a , b , c );
sscanf ("2006:03:18-2006:04:18", "% s -% s ", sztime1, SZ TIME2);

C Language File Operation function (Basic article)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.