0 basic C language self-study perspective: Standard IO_fopen (), fclose ()

After thinking it over a month, I finally Qiaoxia my first pioneer for.

Thousands and thousands of blog, my blog is the first recorded time learning to understand, read for yourself to find, research is now mainly C language and the STM32 . If you can help, it is the best, if I have to write something wrong please correct me, so please repeatedly contrast, for reference only. Second, the record here is not only to learn, I will write down the thoughts and secret circle of friends can not open!

Immediately into the theme


First, what is the file?

  I believe that, even if people come into contact with the C language file input and output, but also to answer: what is the mouse click on the file is not the only thing! In fact, not all right, because the C language program, the file has a broader definition: files usually are named storage area on a disk or hard drive.

To see is the usual, then that will be unusual. For C programs, the device can also be seen as a file! Such as a screen and keyboard. I see my friends here have a question: So C programming language, and what does it matter? What is the importance of the file C program it?

Simply understood that the input file can be necessary to save the data to a file using the program and program output.

 

  Two forms of file 1.1

  In a C program it seems, only two documents form.

  •   Text File
  •        binary file 

A text file that is inside the data are characters. Pass character into corresponding ASCII code stored in memory. The "ABC" are on 'A', 'B', 'C' storage

Binary file that is stored inside the data directly in memory. Without conversion to ASCII code, it can be understood as a machine language (only 0 and 1). For example, into a binary integer of 1000 to 0,010,011,100,010,000 directly stored in memory.

Second, using standard file IO operations

  2.1 standard IO (Standard High-Level the I / 0) What is

    IO standard is a standard I / O function package is standard ANSI C and stdio.h header file created in the definition. Popular terms is that some operations IO library functions, as to how to achieve and I do not know. Today we are in terms of three functions: fopen (), fclose ().

Correspond to the open file, close the file two functions.

    Secondly, there are corresponding standard IO or IO file called bottom IO (Low-Level the I / O) . No need to be concerned about.

  2.2 standard document

    Mentioned at the beginning, some devices are seen as a C program file. So the C language defines three standard documents: the standard input (the INPUT Standard) , standard output (the Output Standard) , standard error (standard error output).

Correspond to the keyboard , screen , screen. However, the standard error output mandatory output to the screen (even redirected to other files displays an error message) . See here do not understand it does not matter, we combine the following function to explain understand.

Standard file and the corresponding file pointer
Standard file File pointer Equipment commonly used
Standard Input stdin keyboard
Standard output stdout monitor  
Standard error output stderr monitor

 

The file pointer is about to be talked about inside the C language function in a particular pointer.

 

 

 

 

 

Three, fopen ()  

Function Description:

Open or create a file and returns a file pointer.

Prototype:

 

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

 FILE is a structure, but the structure is too complex, no need to figure out.

 

 

 Detailed functions:

Parameters. 1 : const char * path, which is a pointer, char show points to a character string, const path can not show the pointer variable to modify the string.

  This string specifies the file path and name you have to prepare to open or create, for example, "I'm very hungry ah .txt"

Parameter 2 : Set the open mode. Input parameters are as follows:

mode meaning
“r” Only read
“w” Only write, no such file is created, the file is empty there
“a” Write only add content behind the original file. No such file create file
“r+” Reading and Writing Files
“w+” Read and write files, no such file is created, the file is empty there
“a+” Read and write files, add content after the original file. No such file create file

He said open member in the present text are

There are also "rb", "wb", "ab", "r + b", "w + b", "a + b", opens a binary file.

path parameter may be a relative path (../fishc.txt) may be an absolute path (/home/FishC/fishc.txt), is given only if the file name does not include the path, it means that the current file folder in the

 

 

 

 

return value:

1. If the file is opened successfully, it returns a pointer to the file FILE structure;

2. If the file open fails, NULL is returned and errno specified error.

 for example:

    * Fp the FILE;                 // definition of a file pointer 
    fp = the fopen ( " C language data .txt " , " R & lt " )      // read-only way to open the text file, and returns a pointer to a text fp

Then we go then you can manipulate files by fp, as to how to achieve the tube. It can be understood as fp now point to an actual txt file.

Moreover, we can change the second parameter used to feel the difference between them.

四、fclose()

Function Description:

Close the file with previously fopen () to open the file

Close to the data buffer will program or files (depending on the previous operation of the document file data is read or output data to a file)

Prototype:

    int fclose(FILE *fp);

Detailed functions:

Parameters: a file pointer to the file to close. For example previously by fopen () the file created for the fp, fp is now representing the file, fp operations to close the file.

return value:

1.0 If the file is closed succeeds, the return value is;

2. If the file is closed fails, the return value is EOF, and set errno to the specified error.

 

Remember to use fclose after the operation is complete file () because fclose was called to the buffer data to the designated place. fopen and fclose best use.

 

    Next, I will continue to say, oh, well fflush functions and their associated code examples my understanding of the buffers and streams. Stay tuned!

 

 

 

 

  

Guess you like

Origin www.cnblogs.com/justice-fly/p/11421080.html