Linux-fopen-fclose-fread-fwrite and other functions
Clearerr (error flag for clearing file streams)
Related functions
Feof
Header file
# Include <stdio. h>
Define functions
Void clearerr (File * stream );
Function Description
Clearerr () clears the error flag used by the file stream specified by the stream parameter.
Return Value
Fclose (close file)
Related functions
Close, fflush, fopen, setbuf
Header file
# Include <stdio. h>
Define functions
Int fclose (File * stream );
Function Description
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.
Return Value
If the file is successfully closed, 0 is returned. If an error occurs, EOF is returned and the error code is saved to errno.
Error Code
Ebadf indicates that the parameter stream is not an open file.
Example
See fopen ().
Fdopen (convert file description to file pointer)
Related functions
Fopen, open, fclose
Header file
# Include <stdio. h>
Define functions
File * fdopen (INT Fildes, const char * mode );
Function Description
Fdopen () converts the file description of Fildes to the corresponding file pointer and returns the result. 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 ().
Return Value
Returns the file pointer to the stream when the conversion is successful. If the Error Code fails, null is returned, and the error code is stored in errno.
Example
# Include <stdio. h>
Main ()
{
File * fp = fdopen (0, "W + ");
Fprintf (FP, "% s \ n", "hello !");
Fclose (FP );
}
Run
Hello!
Feof (check whether the file stream has read the end Of the file)
Related functions
Fopen, fgetc, fgets, fread
Header file
# Include <stdio. h>
Define functions
Int feof (File * stream );
Function Description
Feof () is used to detect whether the end of the file is read, and the ending number 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.
Return Value
A non-zero value indicates that the end of the file has been reached.
Fflush (update buffer)
Related functions
Write, fopen, fclose, setbuf
Header file
# Include <stdio. h>
Define functions
Int fflush (File * stream );
Function Description
Fflush () forces 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.
Return Value
0 is returned for success, EOF is returned for failure, and the error code is stored in errno.
Error Code
The file specified by the stream parameter is not open, or the open status is read-only. For other error codes, see write ().
Fgetc (one character read from the file)
Related functions
Open, fread, fscanf, GETC
Header file
Include <stdio. h>
Define functions
NT fgetc (File * stream );
Function Description
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.
Return Value
GETC () returns the read characters. If EOF is returned, it indicates the end of the file.
Example
# Include <stdio. h>
Main ()
{
File * FP;
Int C;
Fp = fopen ("exist", "R ");
While (C = fgetc (FP ))! = EOF)
Printf ("% C", c );
Fclose (FP );
}
Fgets (read a string from the file)
Related functions
Open, fread, fscanf, GETC
Header file
Include <stdio. h>
Define functions
Har * fgets (char * s, int size, file * stream );
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, finally, null is added as the string.
Return Value
If gets () is successful, the S pointer is returned. If null is returned, an error occurs.
Example
# Include <stdio. h>
Main ()
{
Char s [80];
Fputs (fgets (s, 80, stdin), stdout );
}
Run
This is a test
This is a test
Fileno (returns the file description used by the file Stream)
Related functions
Open, fopen
Header file
# Include <stdio. h>
Define functions
Int fileno (File * stream );
Function Description
Fileno () is used to obtain the file description used by the file stream specified by the parameter stream.
Return Value
Returns the description of a file.
Example
# Include <stdio. h>
Main ()
{
File * FP;
Int FD;
Fp = fopen ("/etc/passwd", "R ");
FD = fileno (FP );
Printf ("FD = % d \ n", FD );
Fclose (FP );
}
Run
FD = 3
Fopen (open a file)
Related functions
Open, fclose
Header file
# Include <stdio. h>
Define functions
File * fopen (const char * path, const char * mode );
Function Description
The parameter path string contains the path and file name of the file to be opened. The parameter mode string represents the stream format.
Mode has the following forms of strings:
R: open a read-only file, which 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 preceding morphological string 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.
Return Value
After the 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
In general, after opening a file, some file reading or writing operations will be performed. If opening a file fails, the subsequent read/write operations will not proceed smoothly. Therefore, in the fopen () then, make error judgment and handling.
Example
# Include <stdio. h>
Main ()
{
File * FP;
Fp = fopen ("noexist", "a + ");
If (FP = NULL) return;
Fclose (FP );
}
Fputc (write a specified character to the file Stream)
Related functions
Fopen, fwrite, fscanf, putc
Header file
# Include <stdio. h>
Define functions
Int fputc (int c, file * stream );
Function Description
Fputc converts parameter C to unsigned char and writes it to the file specified by parameter stream.
Return Value
Fputc () returns the successfully written character, that is, the parameter C. If EOF is returned, the write operation fails.
Example
# Include <stdio. h>
Main ()
{
File * FP;
Char A [26] = "abcdefghijklmnopqrstuvwxyz ";
Int I;
Fp = fopen ("noexist", "W ");
For (I = 0; I <26; I ++)
Fputc (A [I], FP );
Fclose (FP );
}
Fputs (write a specified string into the file)
Related functions
Fopen, fwrite, fscanf, fputc, putc
Header file
# Include <stdio. h>
Define functions
Int fputs (const char * s, file * stream );
Function Description
Fputs () is used to write the string referred to by parameter S to the file referred to by parameter stream.
Return Value
If it succeeds, the number of written characters is returned. If it returns EOF, an error occurs.
Example
See fgets ().
Fread (reads data from a file Stream)
Related functions
Fopen, fwrite, fseek, fscanf
Header file
# Include <stdio. h>
Define functions
Size_t fread (void * PTR, size_t size, size_t nmemb, file * stream );
Function Description
Fread () is used to read data from a 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.
Return Value
Returns the number of nmemb instances actually read.
Additional instructions
Example
# Include <stdio. h>
# Define nmemb 3
Struct Test
{
Char name [20];
Int size;
} S [nmemb];
Main ()
{
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
Freopen (open a file)
Related functions
Fopen, fclose
Header file
# Include <stdio. h>
Define functions
File * freopen (const char * path, const char * mode, file * stream );
Function Description
The parameter path string contains the path and file name of the file to be opened. For details about the parameter mode, see fopen. 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.
Return Value
After the 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.
Example
# Include <stdio. h>
Main ()
{
File * FP;
Fp = fopen ("/etc/passwd", "R ");
Fp = freopen ("/etc/group", "R", FP );
Fclose (FP );
}
Fseek (read/write location of a mobile file Stream)
Related functions
Rewind, ftell, fgetpos, fsetpos, lseek
Header file
# Include <stdio. h>
Define functions
Int fseek (File * stream, long offset, int whence );
Function Description
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.
Parameters
Whence is one of the following:
The offset displacement from the beginning of the seek_set file is the 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 the read/write location to the beginning of the 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
If the call is successful, 0 is returned. If an error exists,-1 is returned. errno stores the error code.
Additional instructions
Unlike lseek (), fseek () returns a read/write location. Therefore, ftell () must be used to obtain the current read/write location.
Example
# Include <stdio. h>
Main ()
{
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 );
}
Run
Offset = 5
Offset = 0
Offset = 10
Ftell (get the reading location of the file stream)
Related functions
Fseek, rewind, fgetpos, fsetpos
Header file
# Include <stdio. h>
Define functions
Long ftell (File * stream );
Function Description
Ftell () is used to obtain the current read/write location of a 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
The file stream of the ebadf parameter is invalid or can be read and written at a movable location.
Example
See fseek ().
Fwrite (write data to a file Stream)
Related functions
Fopen, fread, fseek, fscanf
Header file
# Include <stdio. h>
Define functions
Size_t fwrite (const void * PTR, size_t size, size_t nmemb, file * stream );
Function Description
Fwrite () is used to write data into the 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.
Return Value
Returns the number of nmemb actually written.
Example
# Include <stdio. h>
# Define set_s (x, y) {strcoy (s [X]. Name, Y); s [X]. size = strlen (y );}
# Define nmemb 3
Struct Test
{
Char name [20];
Int size;
} S [nmemb];
Main ()
{
File * stream;
Set_s (0, "Linux !");
Set_s (1, "FreeBSD !");
Set_s (2, "windows2000 .");
Stream = fopen ("/tmp/fwrite", "W ");
Fwrite (S, sizeof (struct test), nmemb, stream );
Fclose (Stream );
}
Run
See fread ().
GETC (one character read from the file)
Related functions
Read, fopen, fread, fgetc
Header file
# Include <stdio. h>
Define functions
Int GETC (File * stream );
Function Description
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.
Return Value
GETC () returns the read characters. If EOF is returned, it indicates the end of the file.
Example
See fgetc ().
Getchar (reads one character from the standard input device)
Related functions
Fopen, fread, fscanf, GETC
Header file
# Include <stdio. h>
Define functions
Int getchar (void );
Function Description
Getchar () is used to read a character from the standard input device. Convert the character from unsigned char to int and return the result.
Return Value
Getchar () returns the read characters. If EOF is returned, an error occurs.
Additional instructions
Getchar () is not a real function, but is defined by the GETC (stdin) Macro.
Example
# Include <stdio. h>
Main ()
{
File * FP;
Int C, I;
For (I = 0li <5; I ++)
{
C = getchar ();
Putchar (C );
}
}
Run
1234
1234
Gets (reads a string from the standard input device)
Related functions
Fopen, fread, fscanf, fgets
Header file
# Include <stdio. h>
Define functions
Char * gets (char * s );
Function Description
Gets () is used to read characters from the standard device to the memory space specified by parameter S until a line break occurs or the end of the file is read, and then null is added as the string.
Return Value
If gets () is successful, the S pointer is returned. If null is returned, an error occurs.
Additional instructions
Because gets () cannot know the size of string s, it is easy to cause security problems of buffer overflow 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.
Example
Refer to fgets ()
Mktemp (generate a unique temporary file name)
Related functions
Tmpfile
Header file
# Include <stdlib. h>
Define functions
Char * mktemp (char * template );
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.
Return Value
After the 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" is not available ";
Example
# Include <stdlib. h>
Main ()
{
Char template [] = "template-xxxxxx ";
Mktemp (Template );
Printf ("template = % s \ n", template );
}
Putc (write a specified character to the file)
Related functions
Fopen, fwrite, fscanf, fputc
Header file
# Include <stdio. h>
Define functions
Int putc (int c, file * stream );
Function Description
Putc () converts the parameter C to unsignedchar and writes 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.
Return Value
Putc () returns the successfully written character, that is, the parameter C. If EOF is returned, the write operation fails.
Example
See fputc ().
Putchar (write specified characters to the standard output device)
Related functions
Fopen, fwrite, fscanf, fputc
Header file
# Include <stdio. h>
Define functions
Int putchar (int c );
Function Description
Putchar () is used to write the C character of the parameter to the standard output device.
Return Value
Putchar () returns the output success character, namely the parameter C. If EOF is returned, the output fails.
Additional instructions
Putchar () is not a real function, but is defined by the putc (C, stdout) Macro.
Example
See getchar ().
Rewind (resetting the file stream's read/write position to start with the file)
Related functions
Fseek, ftell, fgetpos, fsetpos
Header file
# Include <stdio. h>
Define functions
Void rewind (File * stream );
Function Description
Rewind () is used to move the read/write location of the file stream 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 ).
Return Value
Example
Refer to fseek ()
Setbuf (set the buffer of the file Stream)
Related functions
Setbuffer, setlinebuf, setvbuf
Header file
# Include <stdio. h>
Define functions
Void setbuf (File * stream, char * BUF );
Function Description
After opening the file stream and reading the content, call setbuf () to set the buffer zone of the file stream. 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
Setbuffer (set the buffer of the file Stream)
Related functions
Setlinebuf, setbuf, setvbuf
Header file
# Include <stdio. h>
Define functions
Void setbuffer (File * stream, char * Buf, size_t size );
Function Description
After opening the file stream and reading the content, call setbuffer () to set the buffer zone of the file stream. 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
Setlinebuf (set the file stream as a linear buffer)
Related functions
Setbuffer, setbuf, setvbuf
Header file
# Include <stdio. h>
Define functions
Void setlinebuf (File * stream );
Function Description
Setlinebuf () is used to set the no-buffer Io for file streams for behavior. Call: setvbuf (stream, (char *) null, _ iolbf, 0); see setvbuf ().
Return Value
Setvbuf (set the buffer of the file Stream)
Related functions
Setbuffer, setlinebuf, setbuf
Header file
# Include <stdio. h>
Define functions
Int setvbuf (File * stream, char * Buf, int mode, size_t size );
Function Description
After opening the file stream and reading the content, call setvbuf () to set the buffer zone of the file stream. 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 parameter mode has the following types:
_ Ionbf no buffer Io
_ Iolbf no buffer Io Based on Behavior Change
_ Iofbf has no buffer Io at all. If the Buf parameter is a null pointer, no buffer Io is required.
Return Value
Ungetc (write the specified character back to the file Stream)
Related functions
Fputc, getchar, GETC
Header file
# Include <stdio. h>
Define functions
Int ungetc (int c, file * stream );
Function Description
Ungetc () writes 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.
Return Value
If the call succeeds, the return value is a C character. If any error occurs, the return value is EOF.
This article from http://blog.sina.com.cn/s/blog_506a53070100ncn3.html