The fopen () function and Fdopen () function _c language in C language

Source: Internet
Author: User
Tags string format

C language fopen () function: Open a file and return the file pointer
header file:

#include <stdio.h>

fopen () is a commonly used function that opens a file in a specified manner, with the following prototype:

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

The "parameter" path is the file name that contains the path, and mode opens the file mode.

Mode has several ways:

In the POSIX system, the B character is ignored when included with Linux. The new file created by fopen () will have a s_irusr| s_iwusr| s_irgrp| s_iwgrp| s_iroth| S_iwoth (0666) permission, this file permission will also refer to the Umask value.

The difference between binary and text patterns:
In a Windows system, in text mode, a file wraps on behalf of "\ r \ n". If you open the file in text mode and write the newline character "\ n" with functions such as fputs, the function automatically adds "\ r" before "\ n". That is, "\ r \ n" is actually written to the file.
In text mode in a class Unix/linux system, a file wraps on behalf of "\ n". So there is no difference between the text and binary modes in the Linux system.

For more information, see: C language fopen () the difference between opening a text file and a binary file

Some c-compiler systems may not fully provide all of these functions, some C version without "r+", "w+", "A +", and with "RW", "WR", "AR" and so on, readers pay attention to the provisions of the system.

When the return value file is opened successfully, the file pointer to the stream is returned. Returns null if the file fails to open, and the error code exists in errno.

Note: Generally speaking, the file will be read or written after the action, if the file failure, the next reading and writing can not be smoothly, so in fopen () after the error judgment and processing.

After the file operation is complete, you need to close the file, be aware that it will cause memory leaks and problems with the next time you access the file.

After the file is closed, you need to point the file pointer to null, which prevents the free pointer from appearing, causing unnecessary trouble to the entire project, such as FP = NULL.

Instance to open a file and then close the file.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
  FILE * FSTREAM;
  Char msg[100] = "hello! I have read this file. ";
  Fstream=fopen ("Test.txt", "at+");
  if (fstream==null)
  {
    printf ("Open file Test.txt failed!\n");
    Exit (1);
  }
  else
  {
    printf ("Open file Test.txt succeed!\n");
  }
  Fclose (fstream);
  return 0;
}

C language Fdopen () function: Converting a file descriptor to a file pointer
header file:

 #include <stdio.h>

To define a function:

FILE * Fdopen (int fildes, const char * mode);

Function Description: Fdopen () Converts the file descriptor of the parameter fildes to the corresponding file pointer and returns.

The argument mode string represents the flow pattern of the file pointer, which must be the same as the original file description Word read and write mode. Refer to fopen () for the mode string format.

Return value: Returns a file pointer to the stream when the conversion succeeds. Failure returns null and the error code exists in errno.

Example

#include <stdio.h>
Main ()
{
  FILE * fp = fdopen (0, "w+");
  fprintf (FP, "%s\n", "hello!");
  Fclose (FP);

Perform

Hello!

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.