Analysis of C language file operations (2)

Source: Internet
Author: User

To operate a file in C, you must first open the file. Opening the file involves the fopen function. The fopen function is prototype

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

Path indicates the file path, and mode indicates the open mode.

1) for the file path, note that if the absolute path is not explicitly given, the file is under the project directory by default. If you want to provide an absolute path, note the Escape Character '\'. For example, if test.txt is stored in the root directory of the C drive, the file path parameter value should be C: \ test.txt.

2) the mode is mainly composed of six characters: r, w, a, +, B, and t.

R: Read-only. The file must exist.

W: Write-only mode. If the file exists, the original content is cleared. If the file does not exist, the file is created.

A: The append method is used to open a write-only file. Only write operations are allowed. If the file exists, the added content is placed at the end of the file. If the file does not exist, the file is created.

+: Readable and writable

B: open the file in binary mode.

T: open a file in text mode (open the file in text mode by default)

The following are common combinations:

R: open the file in read-only mode and only allow reading. The file must exist. Otherwise, NULL is returned. If the file is opened successfully, the pointer returned is directed to the file header.

R +: open the file in readable and writable mode, and allow reading and writing. The file must exist. Otherwise, NULL is returned. If the file is opened successfully, the pointer returned is directed to the file header.

Rb +: open a file in readable, writable, or binary format, and allow reading and writing. This file must exist. Otherwise, NULL is returned. If the file is opened successfully, the returned Pointer Points to the file header.

Rt +: open the file in readable, writable, and binary format, and allow reading and writing. This file must exist. Otherwise, NULL is returned. If the file is opened successfully, the pointer returned is directed to the file header.

W: open the file in write-only mode. Only write is allowed. If the file exists, the original content of the file will be cleared. If the file does not exist, the file will be created, the pointer returned after opening successfully points to the file header.

W +: open the file in read/write mode and allow read/write. If the file exists, the original content of the file will be cleared. If the file does not exist, the file will be created and the pointer returned after the file is opened successfully points to the file header.

A: open the file in append and write-only mode. Only write is allowed. If the object exists, the appended content is added to the end of the object. If the object does not exist, the object is created. The pointer returned after successful opening points to the file header (note that it is incorrect to point the pointer to the end of the file after the append method is successfully opened in many books or materials)

A +: open a file by append or read/write, and allow read/write. If a read operation is performed, the system reads data from the beginning. If a write operation is performed, the content is added to the end. If the file does not exist, create the file. The pointer returned after opening successfully points to the file header.

Other methods are similar.

Next we will discuss the differences between opening a file in binary mode and text mode.

In fact, there is no big difference between the two methods to open a file. The only difference is that some special characters are processed.

Open the file in text mode. If data is written to the file, if the linefeed '\ n' (ASII value is 10, 0A) is encountered ), it is converted to carriage return-newline '\ r \ n' (ASII value: 13, 10, 0D0A) and saved to the file. If you encounter carriage return-newline, that is, the continuous ASII value is automatically converted to a line break.

This process is not performed when files are opened in binary mode. The above description only exists in windows, and there is no difference in unix.

 

1. Test Program-Detects the initial position of the pointer when the file is opened in append Mode

The hypothetical project directory contains the file test.txt, which contains the string "ABC"

/* Test the fopen function. When the file is opened in append mode, the initial pointer position is 2011.10.5 */# include <stdio. h> # include <stdlib. h> int main (void) {int n; FILE * fp; if (fp = fopen ("test.txt", "a") = NULL) {printf ("can not open file \ n"); exit (0);} n = ftell (fp ); // obtain the offset bytes from the position of fp at this time to the first part of the file. printf ("% d \ n", n); fputs ("test", fp ); n = ftell (fp); printf ("% d \ n", n); fclose (fp); return 0 ;}

Output result:

0
7
Press any key to continue
According to the output results, after opening the file, the pointer is located in the file header, and the file pointer is moved to the end only when content is added to the file.

2. Test Program-detect the differences between opening a file in binary mode and text mode

/* Test the differences between opening a file in binary mode and text mode./1.10.5 */# include <stdio. h> # include <stdlib. h> int main (void) {char ch; int I; char s [] = {'A', 'B', '\ n', 'C '}; FILE * fp1, * fp2; if (fp1 = fopen ("test1.txt", "wt") = NULL) {printf ("can not open file \ n "); exit (0);} if (fp2 = fopen ("test2.txt", "wb") = NULL) {printf ("can not open file \ n "); exit (0) ;}for (I = 0; I <4; I ++) {fputc (s [I], fp1 ); // write data to the file in text mode fputc (s [I], fp2); // write data to the file in binary mode} Fclose (fp1); fclose (fp2); if (fp1 = fopen ("test1.txt", "rt") = NULL) {printf ("can not open file \ n"); exit (0);} if (fp2 = fopen ("test1.txt", "rb") = NULL) {printf ("can not open file \ n"); exit (0);} ch = fgetc (fp1); while (! Feof (fp1) // read data from the file in text mode {printf ("% 02X", ch); ch = fgetc (fp1 );} printf ("\ n"); ch = fgetc (fp2); while (! Feof (fp2) // read data from the file in binary mode {printf ("% 02X", ch); ch = fgetc (fp2 );} printf ("\ n"); fclose (fp1); fclose (fp2); return 0 ;}

After writing data to the file, use ultraeditto open test1.txtand test2.txt in an aggressive manner. The result is as follows:

According to the obtained results, one character 0D is written, that is, '\ R '.

Program output result:

41420A43
41420D0A43
Press any key to continue...

The output content is different when test1.txt is read in either of the following two ways.

We can see that '\ r \ n' is converted for reading in text mode, but this conversion is not performed in binary mode.

Author: Haizi

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.