Basic operations on files in C language

 

In the C language, operations on files are performed using the FILE structure.

Brief introduction of several commonly used operation file functions

1: open file FILE *fopen( const char *filename, const char *mode );   

The first parameter is a pointer type to the filename string constant;

The second parameter specifies the mode in which the file is opened.

File open mode:

r: read, if the file does not exist, the function call fails;

w: Opens an empty file for writing. If the file does not exist, create a file; if the given file already exists, its contents will be emptied;

a: Open the file for writing. If the file does not exist, create a file first; if the file exists, add new data at the end of the file, and do not remove the existing EOF mark before writing the data;

r+: open the file for writing and reading, the file must exist;

w+: write and read, others are the same as w;

a+: Open the file for reading and adding operations, the others are the same as a.

Note: After opening the file, some file reading or writing operations are generally performed. If the file fails to open, the next read and write operations cannot be performed smoothly, so error judgment and processing are generally performed after fopen().

 

  1. #include<stdio.h>  
  2. main()  
  3. {  
  4.       FILE * pFile=fopen("zkd","r");  
  5.       if(pFile==NULL) return;  
  6.       fclose(pFile);  
  7.       return 0;  
  8. }  

 

2:文件的写入 size_t fwrite( const void *buffer, size_t  size, size_t count, FILE *stream )

The first parameter: points to the data to be written to the file;

Second parameter: the size of the item in bytes. The size_t type is actually an unsigned int type;

The third parameter: the maximum number of items to be written;

Note: That is, ensure that the size of the written data is the product of the second parameter and the third parameter.

The fourth parameter: a pointer to the FILE type, obtained through fopen.

 

3: Flush the buffer fflush ( FILE *stream )

C language uses the cache file system for file operations, that is, the system automatically opens up a cache area in memory for each file in use, and the data written from the memory to the disk file is sent to this buffer in the memory first. Until the data in the buffer is full, the data is sent to the disk file together.

    FILE *pFile=fopen("zkd.txt","w");  
    if (pFile==NULL)  
        return 1;  
    fwrite("I love you!",1,strlen("I love you!"),pFile);  
    fflush(pFile);  

4: File close fclose( FILE *stream  )

Closing the file will also write the buffer content to disk at this time, but compared with fflush, if you want to read and write to the file next, you must open the file again.

 

5: File pointer positioning int fseek( FILE *stream, long offset, int origin);

In the C language file operation, there will be a file pointer, which will move the position at any time according to our operation on the file, and always point to the next position to be written;

The second parameter: the offset (compared to the position of the file pointer)

The third parameter: specifies the starting position of the file pointer, which can take three values:

SEEK_CUR: start from the current position of the file;

SEEK_END: ​​start from the end of the file;

SEEK_SET: Start at the beginning of the file.

 

6:文件的读取 size_t fread( void *buffer, size_t size, size_t count, FILE *stream)

The first parameter: points to the buffer where the data is stored;

Others are the same as fwrite();

 

7: Get the file length method long ftell (  FILE *stream )

The function ftell is used to get the offset number of bytes of the current position of the file position pointer relative to the beginning of the file

First use the fseek function to move the file pointer to the end of the file, and then use the ftell function to get the length of the file.

fseek(pFile,0,SEEK_END);

int len=ftell(pFile);

len is the file length

                #include "stdafx.h"  
                #include <stdio.h>  
                #include <string.h>  
                #include <stdlib.h>  
                  
                int _tmain(int argc, _TCHAR* argv[])  
                {  
                    FILE *pFile=fopen("zkd.txt","r");  
                    if (pFile==NULL)  
                        return 1;  
                    fwrite("I love you!",1,strlen("I love you!"),pFile);  
                    fflush(pFile);  
                    fseek(pFile,0,SEEK_END);  
                    int len=ftell(pFile);  
                    char *ch=(char*)malloc(len+1);  
                //  memset(ch,0,100);  
                    rewind(pFile);  
                    fread(ch,1,len,pFile);  
                    ch[len]='\0';  
                    printf("%s",ch);  
                    fclose(pFile);  
                    return 0;  
                }

important point:

The opening and closing of the file are matched, and the opening of the file has the closing of the file.

When reading and writing a file, you need to pay attention to the file position pointer , which controls the position of the file being read and written, such as whether to read and write at the beginning or at the end. For example, when we call fgetc() to read a character, the file position pointer will automatically point to the next byte.

The file pointer and the file location pointer are two different concepts. The file pointer points to the entire file structure, while the file location pointer points to the location where the file is read and written.

 

Reference article: https://blog.csdn.net/clh19901213/article/details/18269989

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325009716&siteId=291194637