How to systematically learn C language documents

Keywords: C Back-end

The program source code we write and the executable files generated by compilation belong to files. Therefore, the essence of a file is a continuous piece of binary data stored on an external storage medium.

c language program processes files in the form of file stream. The program runs in memory, and the files are stored on external storage media, such as hard disk, U disk, etc.

1, Opening and closing of files

When the program is running, a pipeline will be established on the specified file. When reading the file, the data will flow from the file side to the program side like water, and when writing the file, the data will flow from the program side to the file side like water. The file stream from the file side to the program side is called the input stream, and the file stream from the program side to the file side is called the output stream. As shown below:

As long as you open a file, you get a file stream. With the file stream, you can read and write the file accordingly.

In the standard library of c language, there are a series of library functions related to files. If you want to use these library functions, you only need to include the header file "stdio.h" in the program.

1. File opening

The function used to open the file is fopen function. The prototype of this function is:

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

The return value of fopen function is a FILE stream, which is actually a pointer of FILE structure type. This structure variable contains the name, size, attributes and other related information of the FILE; If the execution fails, a null pointer is returned.

Here is an example of opening a file with the fopen function:

FILE *pfile=fopen("D:\\test.txt","r");//Open the test.txt file under disk D in "r" read-only mode
if(pfile!=NULL)
	printf("File opened successfully.\n");
else
	printf("Failed to open file.\n");

It should also be noted that "D:\test.txt" uses an absolute path due to the inclined rod

"\" is used as an escape character in c language, so "\ \" must be used to represent a slash character itself.

2. File closing

Close the file and use the "fclose" function. The function prototype is as follows:

int fclose(FILE *stream);

The function of the fclose function is to close the file stream specified by the function. The return value of the function is of type int. when the function is executed successfully, it returns an integer value of 1. When the function fails, it returns an EOF (file end point), which is usually defined as - 1

2, File reading and writing

The main purpose of opening a file and creating a file stream is to read or write data to the file. Here are several library functions related to file reading and writing and their application examples.

1. Read and write files in the form of characters

The fgetc function and fputc function provided by the standard library can read and write files in the form of characters.

Case: write a program to write "I love you" in characters into the test.txt file on disk D. the code is as follows:

#include<stdio.h>
int main(){
    char str[]="I love you";
    FILE *pfile=fopen("D:\\test.txt","w");
    if(pfile){
        char *ptmp=str;//Temporary pointer to the first character of the array str
        while(*ptmp){	//Dereference ptmp and check whether the character pointed to is an empty character
            fputc(*ptmp,pfile);	//Write the character pointed to by ptmp to the file associated with pfile
            ++ptmp;	//Move the pointer to the next character in the array
        }
        fclose(pfile);	//Close file
        puts("Write to complete.");
    }
    else
        puts("File opening failed.");
    return 0;
}

Read the contents of test.txt file on disk D in the form of characters, store it in the character array, and print out. The program is as follows:

#include<stdio.h>
int main(){
    char buf[128];	//Character array
    FILE *pfile=fopen("D:\\test.txt","r");	//Open in read-only mode
    if(pfile){
        char *p=buf;	//Pointer to the first address of the array p
        while((*p=fgetc(pfile))!=EOF)//Read a character from a file and store it in an array
            ++p;	//Move pointer
        *p='\0';	//Modify EOF characters in the array to null characters
        fclose(pfile);
        printf("The read content is:%s\n",buf);
    }
    else
        puts("File opening failed.");
    return 0;
}

2. Read and write files in line mode

fgets function and fputs function can easily read or write a line of characters to a text file.

Case: write a program to write "I love you" in line to the test.txt file on disk D. the code is as follows:

#include<stdio.h>
int main(){
    FILE *pfile=fopen("D:\\test.txt","w");
    if(pfile){
        if(fputs("I love you",pfile)!=EOF)
            printf("File writen successful.\n");
        else
            printf("Failed to write file.\n");
        fclose(pfile);
    }
    else
        printf("File opening failed.\n");
    return 0;
}

If "I love you" is used to read the test.txt file on disk D in line, the code is as follows:

#include<stdio.h>
int main(){
    char buf[128];	//Character array
    FILE *pfile=fopen("D:\\test.txt","r");	
    if(pfile){
        if(fgets(buf,128,pfile))
            printf("The read contend is:%s\n",buf);
        else 
            printf("Failed to read file.\n");
        fclose(pfile);
    }
    else
        printf("File opending failed.\n");
    return 0;
}

The above two ways of writing and reading have given the complete code, which can be selectively copied to the local for self verification and experience

In general, we usually use less file operations, so we have to learn some other high-level operations in practical application - if we need to use a certain knowledge point, we can learn it immediately. There is no need to apply all the knowledge!

Posted by MasK on Sat, 30 Oct 2021 19:44:48 -0700