Series C language learning: reading and writing files & headers

In this paper we introduce how to use C to create, open, close, text or binary files.

open a file

Use fopen ()  function

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

filename  is a string that names the file access pattern  mode  value may be one of the following values:

mode (Mode) description
r Open an existing text file, allowed to read files.
w Open a text file that allows write to a file. If the file does not exist, a new file is created. Here, your program will write the contents from the beginning of the file. If the file exists, the length is truncated to zero, re-written.
a Open a text file written to a file in append mode. If the file does not exist, a new file is created. Here, your program will be additional content in the existing content file.
r+ Open a text file that allows read and write files.
w+ Open a text file that allows read and write files. If the file already exists, the file will be truncated to zero length, if the file does not exist, a new file is created.
a+ Open a text file that allows read and write files. If the file does not exist, a new file is created. Reading will start from the beginning of the file, it can only be written in append mode.

If the process is a binary file, you need to use the following access mode instead of the above modes of access:

"rb", "wb", "ab", "rb+", "wb+", "ab+",

 

Close the file

Use  fclose ()  function

 int fclose( FILE *fp );

Successfully close the file fclose ()  function returns zero, an error occurs when closing the file, the function returns  EOF . This function is in fact, will clear the data in the buffer, close the file, and releases all memory for the file. EOF is defined in the header file  stdio.h  constants in.

 

Write to file

C standard library provides a variety of functions by characters or to read and write files in the form of fixed-length string.

of fputc ()  the value of the parameter c is written into the character pointed fp output stream:

int fputc( int c, FILE *fp );

If the write succeeds, it returns the character written, if an error occurs, it returns  EOF .

fputs () function string  s  written fp points to the output stream:

int fputs( const char *s, FILE *fp );

fprintf () function writes a string to a file:

 int fprintf(FILE *fp,const char *format, ...) 

 

Read the file

fgetc ()  function reads a character from the input file pointed to by fp:

int fgetc( FILE * fp );

The return value is a character read, if an error occurs returns  EOF .

 fgets ()  function reads n fp from the input stream pointed to - 1 character:

int fputs( const char *s, FILE *fp );

It will copy the string read into the buffer  Buffer , and in the last append a  null  character to terminate the string. If this function before reading the last character you encounter a newline character '\ n' at the end of the file or EOF, only to return to read the characters, including newline.

 the fscanf ()  function to read a string from the file:

int fscanf(FILE *fp, const char *format, ...)

But in the face of the first spaces and line breaks, fscanf () function will stop reading. 

 

Binary input / output functions

The following two functions are generally used to read and write or the like array structure of the memory block:

Read:

size_t fread(void *ptr, size_t size_of_elements,size_t number_of_elements, FILE *a_file);
              

Write: 

size_t fwrite(const void *ptr, size_t size_of_elements,size_t number_of_elements, FILE *a_file);

 

fseek()

This function may be moved to the specified position the file pointer to read, write or insert:

int fseek(FILE *stream, long offset, int whence);

fseek sets the current point to the read-write offset at, The whence may be SEEK_SET, SEEK_CUR, SEEK_END values ​​determined from the file header, the current point and the end of the file offset is calculated offset. By defining a pointer file FILE * fp, when opening a file, the file pointer to the beginning or end of the file the current point, this refers to the number of bytes offset just go control offset (either positive or negative ) on it.

Note: Only opened with r + schema file to insert content, W or w + mode will clear out the contents of the original file to write again, a, or a + mode, i.e. always at the far end add content file, even if moving the file pointer by fseek () position .

 

C header files

Header extension is  .h  file contains macro definitions and function declarations C, is referenced shared by multiple source files. There are two types of headers: header files written by the programmer and the compiler that comes with header files.

The following is the assumption that the name headfile.h header file: 

#ifndef HEADFILE_H//作用:防止headfile.h被重复引用
#define HEADFILE_H
#include<....>//引用标准库的头文件
...
#include"..."//引用非标准库的头文件
...
void Function1(...);//全局函数声明
...
inline();//inline函数的定义
...
classBox//作用:类结构声明
{
...
};
#endif

Header files generally consists of four parts:

(1) the copyright and version statement at the beginning of the file header (#ifndef and #define);

(2) pre-processing block (#include);

(3) defined inline function;

(4) the class structure and function statements.

In the header file is generated by pre-processing block ifndef / define / endif structure, with reference to #include format library header files.

References the header file

Using preprocessing directives  #include  may reference user and system header files.

#include <file>

This form of system used to reference the header files. It searches for a file named file in the standard list of system directory.

#include "file"

This form is used to reference the user header files. It is the first in the directory containing the current file (relative to the program directory) search for a file named file, not just re-search in the system directory.

#include  instructions cause the C preprocessor browse files referenced file as an input to the main routine. I.e., the output of preprocessor compiler can see contains text before has #include, and documents cited  #include  text following the command.

If a header file is referenced twice, the compiler will handle twice the contents of the header file, which will generate an error. To prevent this, the standard practice is to put the entire contents of the file conditional compilation statements (#ifndef, #define and #endif) , the header file headfile.h like to be.

In order to prevent a header file is referenced twice, the compiler will handle twice the contents of the header file, thus generating an error. We need to use conditional compilation statements:  #ifndef .

Selecting a reference to the program from a plurality of different header file:

#if SYSTEM_1
   # include "system_1.h"
#elif SYSTEM_2
   # include "system_2.h"
#elif SYSTEM_3
   ...
#endif

or

#define SYSTEM_H "system_1.h"
 ...
#include SYSTEM_H
Published 161 original articles · won praise 90 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_42415326/article/details/104027465