About the C language file manipulation method

  • 2020-06-03 07:48:35
  • OfStack

I also wrote a tutorial about C language file operation before, but it was not comprehensive at that time, I just simply used 1, and I will learn 1 again today.

1. Document writing

Let's start with a simple example:


include<stdio.h>
int main()
{
 FILE *fp; // The statement 1 A file pointer to the first address of the file buffer  
 char ch;
 if( (fp = fopen("file_1.txt","a")) == NULL ) //w+ Open the read-write file, zero if the file exists, and create a new file if it doesn't ,a  The mode does not clear  
 {
  printf("Cannot open file, press any key to exit!"); // File cannot be opened  
 }
  printf("Input a string:\n");
 while ((ch = getchar()) != '\n' )
 {
  putc(ch,fp); // This function writes characters to a file, and  fputc  The difference between?  
 }
 fclose(fp); 
 return 0;
}

The example is to open or create a new text file and then write the data entered by the user in the console.

The first thing about file operations is:

FILE *fp;

This FILE is the file type identifier, which is a structure type defined by the C compilation system. The structure contains the file name, file state and other information.
It defines a pointer variable, fp, used to point to a file that holds the first address of the file buffer. This pointer is also called a file pointer,

A file pointer is a pointer to the beginning of a file. Using the file pointer, we can open or close a file, at the same time, we can also use the file pointer to move the data read/write location to any byte location of the file.

Then use the fopen function to open or create a new file:

The fopen() function is used to open a file that takes two string arguments: file name (filename) and open mode (open mode)
fopen("filename","open mode")

When using this function to open a file, it returns a pointer to the beginning of the file, called the file pointer, so in order to receive the file pointer it returns, we need to declare a file pointer in advance: FILE *fp

If the file cannot be opened for some internal system reason, the function returns a null pointer to NULL

Therefore, in order to avoid program exceptions caused by file opening failure, open file 1 in the following format:


 if( (fp = fopen("file_1.txt","a")) == NULL ) //w+ Open the read-write file, zero if the file exists, and create a new file if it doesn't ,a  The mode does not clear  
 {
  printf("Cannot open file, press any key to exit!"); // File cannot be opened  
 }

About the file open mode, I was at my 1 file operations blog was introduced in detail in the article, link: https: / / www ofstack. com article / 135845. htm

Here is getting user input, using 1 while loop continuously getting input,


 while ((ch = getchar()) != '\n' )
 {
  putc(ch,fp); 
 }

Use \n as the end flag, that is, only one line of string can be entered on the console, or multiple lines if EOF is used, with ctrl+z for the end, and the key combination for the end of the standard input file.

Under 1 sentence putc(ch,fp);  User input character to the file, note that this is 1 character to 1 character, because the input is using the getchar function, if it is a string input to write, you can use the fputc () function directly to write a string.

The last and very important part is to close the file after reading and writing to prevent the file reading and writing chaos caused by opening too many files.

Close 1 file using the fclose () function,

Close the file, return 0 when normally closed, or return 1

When closed, the system clears the buffer and outputs the data to the disk file, freeing the buffer unit and decoupling the file pointer from the specific file.

fclose(fp);   

2. Read files

So this is basically what the write operation is about, and this is what the read operation is about, which is pretty much the same as the write operation.

Let's look at the complete code first, so as to compare it with the above operation:


#include <stdio.h>
#include <stdlib.h>
int main()
{
 FILE *fp;
 char ch; 
 char str[80];
 char file_name[15]; // Enter the file name buffer  
 // If the file and this procedure are in the same 1 Under the file, then directly input txt File name is ok  
 // If it's not the same 1 Below the directory, enter the full file path format  
 // Such as   :  e:\\test.txt 
 printf("please enter the filename: "); // Note that you need to include the suffix of the file when you enter the file name .txt 
 gets(file_name); //gets Function to read the user input string, specifically designed for string processing  
  if((fp = fopen(file_name,"r")) == NULL ) //r Mode cannot write data, only read it  
 {
  printf("Cannot open file, press any key to exit!\n"); // File cannot be opened  
  exit(0); 
 }
/* 
 while( (ch = getc(fp)) != EOF )  //getc The function reads a single character from a file  
 {
  putchar(ch);     // Looping out a single character  
 }
 putchar('\n'); // The output 1 A line feed  
*/ 
 while((fgets(str,80,fp)) != NULL) 
  puts(str);   
 fclose(fp);
}

The beginning of the open file and write operation is similar, is more than 1 user input file path and name.

The program then reads the contents of the file in two ways, one using the fgetc function to read a single character, then outputs, and one looping until it reaches the end of the file.

Another way is to use the special fgets () function to read 80 bytes at a time:

The fgets function reads 80 bytes of data from the fp file into the str array and outputs it with the puts function.

When the end of the file is reached, the function returns NULL and, to continue reading the string, loops to detect whether the end of the file has been reached after 80 bytes each read.

Above is the site to you about the C language file operation method, I hope to help you, if you have any questions welcome to leave a message, this site will reply you in time!


Related articles: