C++

Several header files and library functions commonly used in the C language


Incomplete statistics, C language there are as many as 15 in the standard library header files, so I mainly introduce the commonly used the four header files stdio. h, string. h, math. h, stdlib. h, after used the other to do added. The following dry goods:

1. < stdio.h > : defines input/output functions, types, and macros. Functions make up almost a third of the standard library.

(1) file access.

FILE *fopen (" filename "," mode ") :

Open the file with the address ‘filename’ in mode mode and return the file pointer.

Access mode is mainly “r” : read only; “w” : write only and delete existing content; “a” : append, append at the end;

“r+”, “w+” : reading and writing; “a+” is appended, and “b” is added at the end above: base 2 file operation.

Note: where r is an open file, w is created (if the file does not exist); w overrides the original, and a appends to the end of the original file.

int fclose ( FILE *f ) :

Release the buffer data and close the stream.

Here are two that I haven’t used much:

FILE *freopen (" filename "," mode ", FILE * f ) :

Open the file with the address ‘filename’ in mode mode and associate it with the stream f2.

int fflush ( FILE *f ) :

Writes to a file all data that has been written to a buffer but not to a file.

(2) base 2 input/output

fread ( *ptr . size . n . FILE* f ) :

Read n objects of length size from f and put them into an array that ptr points to.

fwrite ( *ptr . size . n . FILE* f ) :

Read n objects of length size from the ptr pointing array and write them to f.

Note: be aware of the write and read objects. Both reads and writes are for the f file stream.

(3) unformatted input/output

int fgetc/getc ( FILE *f ) :

Returns the next character of the stream f, reaches the end of the file/error, returns EOF.

int fputc/putc ( int c .  FILE *f )

Enter the character c into the stream f.

int fgets ( char * s . int n . FILE *f ) :

Read the next n-1 character in f into the array s. When a newline character is encountered, it terminates.

int fputs ( const char * s . FILE *f ) :

Output the string s to the stream.

int fclose ( FILE *f ) :

0

Write c back into f.

(4) format input/output

Output in 1 format.

scanf/fscanf/sscanf
printf/fprintf/sprintf

Note: without f/s, it is the format string standard input/output function, input/output to terminal/screen; printf (const char *format…)

Add s, input/output to the string. sprintf (char *s, const char *format,…)

Add f, input/output to file stream. fprintf (FILE *f, const char *format,…)

(5) file location

long ftell ( FILE *f ) :

Returns the current pointer position. Return -1L on error.

int fclose ( FILE *f ) :

3

Set the current stream f pointer position to offset characters after origin. origin: SEEK_SET: file start location; SEEK_CUR pointer current location, SEEK_END: end of file location.

int fclose ( FILE *f ) :

4

Pointer returns the starting position of the file, equivalent to: fseek (f, 0L, SEEK_SET)

Not commonly used:

int fclose ( FILE *f ) :

5

Record the current position of the pointer to *p for use by the fsetpos function.

int fclose ( FILE *f ) :

6

The pointer position points to *p.

(6) error handling and file operation

int feof ( FILE *f )

Indicates the end of the file, with 1 returning 0 and a non-0 value at the end.

int ferror ( FILE *f )

If the error indicator associated with f is set, a non-zero value is returned.

int fclose ( FILE *f ) :

9

Deletes the file at the specified location.

int rename ( const char *oldname . const char *newname )

Change the file name, otherwise return a non-zero value.

FILE *tmpfile ( void )

Create a temporary file in ‘wb+’ mode and delete it automatically when it is closed or the program ends normally

2. < string.h > Divide into two groups, group 1 begins with str and group 1 begins with mem < memory.h > Not all compilers have them.)

(1) str

char * strcpy (char *dest, const char *src);

Copy the string src to the string dest (including ‘\0’) and return dest

char * strncpy (char *dest, const char *src . n);

Copy the former n characters from src into dest, return dest, and ‘\0’ will be added if there are not enough n characters.

char * strcat (char *dest, const char *src);
char * strncat (char *dest, const char *src . n);

After connecting src to dest; The first n characters in src are connected to dest.

int  strcmp (const char *s1, const char *s2);
int  strncmp (const char *s1, const char *s2 . n);  To compare s1 with s2 . s1<s2 Returns a negative number, otherwise 0 ; Compare the former n A character
char * strchr (const char *s, int c);
char * strrchr (const char *s, int c);  Return string s In the first 1 Secondary occurrence character c Position pointer; Return string s In the last 1 Secondary occurrence character c Position pointer ( c Will be converted into a char )

size_t strcspn (const char *s1, const char *s2); char * strdup (const char *s); char * strerror (int errnum);

(2) mem

void *memcpy ( void *s .  const void *ct . n )

Copy the first n characters from the string ct into s and return s.

void *memmove ( s . ct . n The above) 1 Function, but it still executes correctly when the objects overlap.
void *memset ( void *s . int c .  n )

Replace the first n characters in s with c and return s.

int memcmp ( cs . ct . n )

n characters before cs are compared with ct, cs < ct, returns a negative number; Otherwise return 0

void *memchr ( cs . c . n )

Returns a pointer to the first occurrence of c in cs.

3. < math.h > 1 some mathematical functions

Beg absolute value

int abs  (int x);
long labs (long x);
float fabs  ( float x ) :

All return values are double, the Angle of the three-angle function is expressed in radians, x, y are all double

double acos (x);
double asin (x) ;
double atan (x);
double atan2 (y, x) :   arctan ( y/x )
double tan  (x);
double tanh (x) :      Hyperbolic tangent
double sin  (x);
double sinh (x);
double cos  (x);
double cosh (x);
double pow  (x, y) ; // Note that the return value is of type double precision
double sqrt (x);
double ceil (x) :     Not less than x The minimum integer number of (round up)
double floor (x) :    No greater than x Of the maximum integer type (rounded down)
double exp  (x);
double fabs (x);
double log  (x) :      ln ( x )
double log10 (x);
double fmod (x, y) :     x/y The remainder of
double ldexp (x, n) :          x*2^n
double modf (x, double *ip):    will x Divide into integer and decimal parts, return the decimal part, and put the integer part *ip In the
double frexp (x, int *exp);     will x Divided into ( 0.5,1 ) between the 1 The sum of real fractions 1 a 2 Omega to the power of omega, which returns the true fraction, omega to the power of omega exp In the

4. < stdlib.h > Numerical conversion, memory allocation and so on.

int rand ( void )

Produces a random integer between 0 and 32767.

Pointer name = (data type *) calloc (n, size)

Allocate n contiguous Spaces of length size and set all elements to zero to return the first address.

Pointer name = (data type *) malloc (n*size)

Allocate n*size bytes of memory and return the first address.

Pointer name p1= (data type *) realloc (pointer name p2 to change memory size, new size n)

Allocate the space with the length of n bytes, assign the value of p2 to this memory, and return the first address to p1. (change the memory size of p1 to the address)

void free ( *p )

Free the memory pointed by p (p is the memory allocated by malloc, realloc, calloc)

void abort  ( void )

Cause the program to terminate abnormally.

void exit ( int status )

Cause the program to terminate normally.

conclusion