fopen (), fclose () Open/close file

Source: Internet
Author: User

Open/close/refresh Stream

1. fopen () Open stream

Function:

1) fopen () opens a file specified by path.

2) Fdopen () gets a first file descriptor and combines a standard I/O stream with the description. This function is commonly used for descriptors that are returned by creating pipelines and network communication functions that are small. Because these special types of files cannot be opened with the standard I/O fopen function, we must first invoke the device-specific function to obtain a file descriptor, and then use fopen to associate a standard I/O stream with the description typeface.

3) Fropen () opens a specified file on a specified stream, and if the stream is already open, close the stream first. If the stream is already directed, this fopen clears the orientation. Eating functions are typically used to turn a specified file into a predetermined stream, such as: standard input, standard output, and standard error.

Required header File:<stdio.h>

Function Prototypes:

FILE *fopen (const char *path, const char *mode);

File *freopen (const char *path, const char *mode, file *stream);

FILE *fdopen (int fd, char *mode);

Parameters:

Fopen:path the absolute path of the specified file that will be opened, and how the mode file is opened.

Freopen:path the absolute path of the file to be redirected, the mode file opens in the way that stream has been opened by the stream.

FDOPEN:FD has opened the file descriptor, mode file open mode.

return value:

Success: Stream pointer failed: NULL

Parameter mode:

R,RB: Open File as read-only, file must already exist

corresponding file I/o:open (path, o_rdonly);

r+,rb+: Open file in read/write mode, file must exist

W,WB: Open the file as write-only, empty the file if it exists, and create the file if it does not exist.

w+,wb+: Open the file read-write, if the file exists, the contents of the file is emptied, if the file does not exist, the file is created.

A,ab: Write only open file in Append mode, if the file does not exist, then create the file, if the file exists, the written data will be appended to the end of the file, the original content of the file is retained.

a+,ab+: The open file can be read and written in append mode, if the file does not exist, the file is created, if the file exists, the written data is appended to the end of the file, and the original contents of the file are retained.

Frepen () redirection Use example

1#include <stdio.h>2 intMainintargcChar*argv[])3 {4FILE *FP;5     if(ARGC <2)6 {7fprintf ("usage:%s filename\n", argv[0]);8       return-1;9 }TenFreopen (argv[1],"W", stdout); Redirects the standard input to the file specified by argv[1].  Onefprintf ("After freopen!\n"); A     return 0; - } -The output will be argv[1] in the specified file

2. Fclose () Close the stream

Function: Close a file stream

Required Header Files <stdio.h>

Function Prototypes:

int fclose (FILE *fp);

Parameters:

FP: the stream to close.

return value:

The call returns 0 successfully, and the failure returns EOF and sets the errno

It is worth noting that fclose () outputs the data in the buffer before closing the stream, freeing the buffer if the standard I/O library has allocated a buffer for the stream.

When a process terminates normally (calling exit () function directly terminates the process without flushing the stream, or returning from the main function), all standard I/O with <_exit cache data is flushed and all the standard I/O for the substitute are closed.

3. Refresh the Stream

Function: Forces a stream to be refreshed. This function makes all the data that is not written to the stream be passed to the kernel.

Function Prototypes:

int fflush (FILE *fp);

Parameters:

FP: The stream to be refreshed.

fopen (), fclose () Open/close file

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.