Explain the usage of freopen () function and fclose () function in C language _c language

Source: Internet
Author: User

C language Freopen () function: Open the file function and get the file handle

Header file:

#include <stdio.h>

To define a function:

FILE * Freopen (const char * path, const char * mode, FILE * stream);

Function Description:
The parameter path string contains the file path and file name that you want to open.
Parameter mode please refer to the fopen () description ...
The parameter stream is an open file pointer. Freopen () closes the file stream that was opened by the original stream, and then opens the file for the parameter path.

Return value: After the file is successfully opened, the file pointer to the stream is returned. Returns null if the file fails to open, and the error code is in errno.

Example

#include <stdio.h>
Main ()
{
  FILE * FP;
  fp = fopen ("/etc/passwd", "R");
  fp = freopen ("/etc/group", "R", FP);
  Fclose (FP);

C language Fclose () function: Close Open File
header file:

#include <stdio.h>

To define a function:

int fclose (FILE * stream);

Function Description: fclose () is used to close a previously fopen () Open file. This action will cause the data in the buffer to be written to the file and release the file resources provided by the system.

Return value: Returns 0 if the file is successful, returns EOF when an error occurs, and saves the error code to errno.

Error code: EBADF represents a file that is not open for parameter stream.

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.