C file Function

Source: Internet
Author: User
Tags define function rewind

Void clearerr (File * stream); The feof function indicates the error flag used by clearerr () to clear the file stream specified by the stream parameter.
Int fclose (File * stream ); Related functions close, fflush, fopen, and setbuf indicate that fclose () is used to close files opened by fopen. This action writes data in the buffer zone to a file and releases the file resources provided by the system. If the object is successfully closed, 0 is returned. If an error occurs, EOF is returned and the ErrorCodeSave to errno. The error code ebadf indicates that the parameter stream is not an open file. For more information, see fopen ().

File * fdopen (INT Fildes, const char * mode ); Related functions fopen, open, and fclose indicate that fdopen () will convert the file description of Fildes to the corresponding file pointer and return. The mode string represents the stream format of the file pointer, which must be the same as the read/write mode of the original file description. For more information about the mode string format, see fopen (). If the return value is converted successfully, the file pointer pointing to the stream is returned. If the Error Code fails, null is returned, and the error code is stored in errno.
Int feof (File * stream ); The fopen, fgetc, fgets, and fread functions indicate that feof () is used to detect whether the end of the file is read, and the ending stream is the object pointer returned by fopen. If the end of the file is reached, a non-zero value is returned. Otherwise, 0 is returned. The return value is a non-zero value, indicating that the object has reached the end of the object.
Int fflush (File * stream ); The related functions write, fopen, fclose, and setbuf indicate that fflush () will force the data in the buffer to be written back to the file specified by the parameter stream. If the parameter stream is null, fflush () updates all open file data. If the return value is successful, 0 is returned. If the return value is failed, EOF is returned. The error code is stored in errno. The file specified by the error code "stream" is not opened, or the file is in read-only status. For other error codes, see write ().
Int fgetc (File * stream ); The open, fread, fscanf, and GETC functions indicate that fgetc () reads a character from the file indicated by the stream parameter. If you read the end Of the file without data, the EOF is returned. If the returned value is GETC (), the read characters are returned. If the returned value is EOF, the returned values are at the end of the file.
Char * fgets (char * s, int size, file * stream ); (Read a string from the file) related functions include <stdio. h> Function Description: fgets () is used to read characters from the file referred to by the parameter stream to the memory space referred to by the parameter S, until a line break occurs, the end of the file is read, or the size-1 character is read, null is added as the string. If gets () is returned successfully, the S pointer is returned. If null is returned, an error occurs.
Int fileno (File * stream ); (Return the file description term used by the file stream) related functions open, fopen function description fileno () is used to obtain the file description term used by the file stream specified by the parameter stream. The Return Value Returns the description of the file.
File * FP; int FD; FP = fopen ("/etc/passwd", "R"); FD = fileno (FP ); printf ("FD = % d \ n", FD); fclose (FP); execute FD = 3
File * fopen (const char * path, const char * mode ); (Open File) related functions open, fclose function description parameter path string contains the file path and file name to open, the parameter mode string represents the stream form. Mode has the following types of strings: R. To open a read-only file, the file must exist. R + open a readable file, which must exist. W. Open and write only the file. If the file exists, the file length is 0, indicating that the file content will disappear. If the file does not exist, the file is created. W + open the readable and writable file. If the file exists, the file length is cleared to zero, that is, the file content disappears. If the file does not exist, the file is created. A. Open and write-only files as an attachment. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. A + opens readable and writable files by appending them. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. The form string mentioned above can be added with a B character, such as a combination of Rb, W + B, and AB +. The B character is added to indicate that the file opened by the function library is a binary file, rather than text files. However, this character is ignored in the POSIX System in Linux. The new file created by fopen () has the s_irusr | s_iwusr | s_irgrp | s_iwgrp | s_iroth | s_iwoth (0666) Permission. For the File Permission, see umask value. When the returned value file is successfully opened, the file pointer pointing to the stream will be returned. If the file fails to be opened, null is returned and the error code is stored in errno. Note: After opening a file, the file will be read or written. If opening the file fails, the subsequent read/write operations will not be smooth. Therefore, in the fopen () then, make error judgment and handling.
Int fputc (int c, file * stream ); (Write a specified character to the file stream) the related functions fopen, fwrite, fscanf, and putc indicate that fputc will convert parameter C to unsigned char and write it to the file specified by parameter stream. If the returned value is fputc (), a successfully written character is returned, that is, the parameter C. If EOF is returned, the write operation fails.
Int fputs (const char * s, file * stream ); (Write a specified string into the file) fopen, fwrite, fscanf, fputc, putc function description fputs () it is used to write the string referred to by parameter S into the file referred to by parameter stream. If the return value is successful, the number of written characters is returned. If the return value is EOF, an error occurs. For an example, see fgets ().

Size_t fread (void * PTR, size_t size, size_t nmemb, file * stream ); (Read data from the file stream) related functions fopen, fwrite, fseek, fscanf define function functions description fread () is used to read data from the file stream. The stream parameter is an opened file pointer. the PTR parameter points to the data space to be read. The number of characters read is determined by the size * nmemb parameter. Fread () returns the number of nmemb actually read. If this value is smaller than the nmemb parameter, it indicates that the file may be read at the end or an error occurs. In this case, feof () must be used () or ferror () to determine what happens. The Return Value Returns the number of nmemb actually read. # Define nmemb 3 struct test {char name [20]; int size;} s [nmemb]; file * stream; int I; stream = fopen ("/tmp/fwrite ", "R"); fread (S, sizeof (struct test), nmemb, stream); fclose (Stream );
For (I = 0; I <nmemb; I ++) printf ("name [% d] = %-20 s: Size [% d] = % d \ n ", i, S [I]. name, I, S [I]. size );
Run name [0] = Linux! Size [0] = 6 name [1] = FreeBSD! Size [1] = 8 name [2] = Windows2000 size [2] = 11
File * freopen (const char * path, const char * mode, file * stream ); (Open File) related functions fopen, fclose function description parameter path string contains the file path and file name to open, parameter mode please refer to fopen () description. The stream parameter is an opened file pointer. Freopen () will close the file stream opened by the original stream, and then open the file with the path parameter. When the returned value file is successfully opened, the file pointer pointing to the stream will be returned. If the file fails to be opened, null is returned and the error code is stored in errno.
Int fseek (File * stream, long offset, int whence ); (read/write location of a mobile file stream) related functions rewind, ftell, fgetpos, fsetpos, the lseek function indicates that fseek () is used to move the read/write location of a file stream. The stream parameter is an opened file pointer, And the offset parameter is the number of read/write locations to be moved Based on the whence parameter. The whence parameter is one of the following: the offset displacement from the beginning of the seek_set file is a new read/write location. Seek_cur increases the offset displacement at the current read/write position. Seek_end points the read/write position to the end of the file and then increases the offset displacement. When the whence value is seek_cur or seek_end, the offset parameter allows negative values. The following are special usage methods: 1) To move a read/write location to the beginning of a file: fseek (File * stream, 0, seek_set); 2) to move the read/write location to the end of the file: fseek (File * stream, 0, 0seek_end); Return Value: 0 if the call is successful; if an error exists,-1 is returned, errno stores error codes. Note that fseek () does not return the read/write location like lseek (). Therefore, ftell () must be used to obtain the current read/write location.
file * stream; long offset; fpos_t Pos; stream = fopen ("/etc/passwd", "R"); fseek (stream, 5, seek_set ); printf ("offset = % d \ n", ftell (Stream);
rewind (Stream); fgetpos (stream, & Pos ); printf ("offset = % d \ n", POS);
Pos = 10; fsetpos (stream, & Pos); printf ("offset = % d \ n ", ftell (Stream); fclose (Stream);
execute offset = 5 offset = 0 offset = 10
Long ftell (File * stream ); (Get the reading location of the file stream) related functions fseek, rewind, fgetpos, fsetpos define the function description ftell () is used to obtain the current read/write location of the file stream. The stream parameter is an opened file pointer. Return Value: if the call is successful, the current read/write location is returned. If an error occurs,-1 is returned, and errno stores the error code. Error code: file stream with invalid parameter stream or removable read/write location. For an example, see fseek ().
Size_t fwrite (const void * PTR, size_t size, size_t nmemb, file * stream ); (Write data to a file stream) related functions fopen, fread, fseek, and fscanf description fwrite () are used to write data to a file stream. The stream parameter is an opened file pointer. the PTR parameter points to the data address to be written. The total number of characters written is determined by the parameter size * nmemb. Fwrite () returns the number of nmemb actually written. The Return Value Returns the number of nmemb actually written.
Int GETC (File * stream ); (One character is read from the file) the related functions read, fopen, fread, and fgetc indicate that GETC () is used to read a character from the file referred to by the parameter stream. If you read the end Of the file without data, the EOF is returned. Although GETC () and fgetc () act the same way, GETC () is defined as a macro and is not a real function call. If the returned value is GETC (), the read characters are returned. If the returned value is EOF, the returned values are at the end of the file. For an example, see fgetc ().
Int getchar (void ); (One character is read by the standard input device) the related functions fopen, fread, fscanf, and GETC indicate that getchar () is used to read a character from the standard input device. Convert the character from unsigned char to int and return the result. If the returned value is getchar (), the read characters are returned. If the returned value is EOF, an error occurs. Additional descriptions getchar () is not a real function, but is defined by the GETC (stdin) Macro.
Char * gets (char * s ); (A string is read by the standard input device.) the related functions fopen, fread, fscanf, and fgets indicate that gets () are used to read characters from the standard device and coexist to the memory space referred to by parameter S, until a line break occurs or the end of the file is read, and null is added as the string. If gets () is returned successfully, the S pointer is returned. If null is returned, an error occurs. Note: Because gets () cannot know the size of string s, it is easy to cause a buffer overflow security problem because it must end the input only when a newline character or the end of the file is encountered. We recommend that you use fgets () instead. For more information, see fgets ()
Char * mktemp (char * template ); (Generate a unique temporary file name) the relevant function tmpfile header file # include <stdlib. h> function description mktemp () is used to generate a unique temporary file name. The last six characters in the file name string referred to by the template parameter must be xxxxxx. The generated file name is returned by a string pointer. When the returned value file is successfully opened, the file pointer pointing to the stream will be returned. If the file fails to be opened, null is returned and the error code is stored in errno.
Additional instructions: the file name string referred to by the template parameter must be declared as an array, for example, char template [] = "template-xxxxxx"; char * template = "template-xxxxxx" cannot be used ";
Int putc (int c, file * stream ); (Write a specified character to a file) the related functions fopen, fwrite, fscanf, and fputc indicate that putc () will convert the parameter C to unsigned char and write it to the file specified by the parameter stream. Although putc () works the same as fputc (), putc () is defined as a macro and is not a real function call. The return value putc () will return the successfully written character, that is, the parameter C. If EOF is returned, the write operation fails. For an example, see fputc ().
Int putchar (int c ); (Write the specified character to the standard output device) related functions include fopen, fwrite, fscanf, and fputc. putchar () is used to write the C character of the parameter to the standard output device. If putchar () is returned, the output is successful, that is, the parameter C. If EOF is returned, the output fails. Additional descriptions putchar () is not a real function, but is defined by the putc (C, stdout) Macro. For more information, see getchar (). Void rewind (File * stream ); (Resetting the file stream's read/write position starts with the file) related functions fseek, ftell, fgetpos, and fsetpos indicate that rewind () is used to move the file stream's read/write position to the beginning of the file. The stream parameter is an opened file pointer. This function is equivalent to calling fseek (stream, 0, seek_set ). For the return value example, see fseek ()

Void setbuf (File * stream, char * BUF ); (Set the buffer of the file stream) The setbuffer, setlinebuf, and setvbuf functions indicate that setbuf () can be called to set the buffer of the file stream before opening the file stream and reading the content. The parameter stream is the specified file stream, and the parameter Buf points to the starting address of the custom buffer. If the Buf parameter is a null pointer, no buffer Io is required. Setbuf () is equivalent to calling: setvbuf (stream, Buf, Buf? _ Iofbf: _ ionbf, bufsiz) Return Value
Void setbuffer (File * stream, char * Buf, size_t size );(Set the buffer of the file stream) related functions setlinebuf, setbuf, and setvbuf indicate that setbuffer () can be called to set the buffer of the file stream before opening the file stream and reading the content. The parameter stream is the specified file stream. The parameter Buf points to the starting address of the custom buffer. The parameter size is the buffer size. Return Value

Void setlinebuf (File * stream ); (Set the file stream as a linear buffer) related functions setbuffer, setbuf, and setvbuf indicate that setlinebuf () is used to set the file stream for non-buffered Io based on the behavior. Call: setvbuf (stream, (char *) null, _ iolbf, 0); see setvbuf (). Return Value
Int setvbuf (File * stream, char * Buf, int mode, size_t size ); (Set file stream buffer) related functions setbuffer, setlinebuf, and setbuf indicate that setvbuf () can be called to set the file stream buffer before opening the file stream and reading the content. The parameter stream is the specified file stream. The parameter Buf points to the starting address of the custom buffer. The parameter size is the buffer size, the mode parameter has the following types: _ ionbf, no buffer IO _ iolbf, no buffer IO _ iofbf, no buffer Io. If the Buf parameter is a null pointer, no buffer Io is required. Return Value
Int ungetc (int c, file * stream ); (write the specified character back to the file stream) related functions: fputc, getchar, and GETC: ungetc () write the parameter C back to the file stream specified by the parameter stream. This write-back character is obtained by the next function that reads the file stream. If the return value is successful, the C character is returned. If an error exists, the EOF is returned.

Related 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.