C language fopen () function:open a file and return the file pointerHeader file:


fopen () is a commonly used function,Used to open the file in the specified way,Its prototype is:

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

[Parameter] path is the name of the file containing the path,mode is the file opening mode.

There are the following modes:

The b character is ignored on posix systems, including Linux. New files created by fopen () will have s_irusr | s_iwusr | s_irgrp | s_iwgrp | s_iroth | s_iwoth (0666) permissions. This file permissions will also refer to the umask value.

Difference between binary and text mode:

In windows system, in text mode,The file uses "\ r \ n" for line breaks.If you open the file in text mode,When writing a newline character "\ n" with functions such as fputs, the function will automatically add "\ r" before "\ n". That is, "\ r \ n" is actually written to the file.

In text mode in a Unix-like system,The file is "\ n" for line breaks.So there is no difference between text mode and binary mode in Linux system.

For more information, please see:the difference between c language fopen () to open text files and binary files

Some c compilation systems may not fully provide all of these functions,Some versions of C do not use "r +", "w +", "a +", but use "rw", "wr", "ar", etc. The reader pays attention to the rules of the system used.

[Return value] After the file is successfully opened,A file pointer to the stream is returned.If the file fails to open, it returns null and stores the error code in errno.

Note:In general,After the file is opened, some file reading or writing actions will be performed.If file opening fails,The subsequent read and write operations cannot be performed smoothly,So please make an error judgment and handle after fopen ().

After the file operation is completed,Need to close the file,It ’s important to note thatFailure to do so will cause memory leaks in the files and problems the next time you access the files.

After the file is closed,Need to point the file pointer to null,Doing so will prevent sparse pointers,And cause unnecessary trouble to the whole project,If fp=null.

[Example] Open a file and then close the file.

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);
    printf ("open file test.txt succeed! \ n");
  fclose (fstream);
  return 0;

c language fdopen () function:convert file descriptor to file pointerHeader file:


Define the function:

file * fdopen (int fildes, const char * mode);

Function description:fdopen () will describe the file descriptor of the parameter fildes, Returns after converting to the corresponding file pointer.

The parameter mode string represents the stream form of the file pointer. This pattern must be the same as the original file descriptor mode. Please refer to fopen () for the mode string format.

Return value:Returns a file pointer to the stream when the conversion is successful. If it fails, it returns null, and stores the error code in errno.


main ()
  file * fp=fdopen (0, "w +");
  fprintf (fp, "%s \ n", "hello!");
  fclose (fp);

carried out

  • Previous Bullet Generation and Collision and Explosion Effect for Android Combat Airplane Game (5)
  • Next Android programming method to achieve custom system menu background