C Standard Library: stdio. H, ctype. H, stdlib. H, assert. H, stdarg. H, time. H <stdio. h>: File Operations
Int remove (const char character extension lename)
• Removes the role le from the fi lesystem.
• Retrn non-zero on error.
Int Rename (const char returns oldname, const char returns newname)
• Renames role le
• Returns non-zero on error (reasons? : Permission, existence)
The first time I found that C's <stdio. h> contains these two functions, I have always thought that file operations are complicated and mysterious. Now it is quite simple. However, the underlying layer should involve security mechanisms, especially when multiple operating systems are involved, different security policies may cause a lot of trouble.
<Stdio. h>: Temporary les
FILE Upload tmp role le (void)
• Creates a temporary using le with mode "wb + ".
• The specified le is removed automatically when program terminates.
Char character tmpnam (char s [L_tmpnam])
• Creates a string that is not the name of an existing role le.
• Return reference to internal static array if s is NULL. Populate s otherwise.
• Generates a new name every call.
It is most appropriate to name this temporary name by time. \
<Stdio. h>: raw I/O
Size_t fread (void upload ptr, size_t size, size_t nobj, FILE Upload stream)
• Reads at most nobj items of sizesize from stream into ptr.
• Returns the number of items read.
• Feof and ferror must be used to test end of role le.
Size_t fwrite (const void upload ptr, size_t size, size_t nobj, FILE Upload stream)
• Write at most nobj items of size from ptr onto stream.
• Returns number of objects written.
Fread reminds me of the forced type conversion for the obtained memory in malloc. This is probably the same way. I will divide the new memory segments by size and then register the type of the memory segments.
<Stdio. h>: file position
Int fseek (FILE into stream, long offset, int origin)
• Sets fi le position in the stream. Subsequent read/write begins at this location
• Origin can be SEEK_SET, SEEK_CUR, SEEK_END.
• Returns non-zero on error.
Long ftell (FILE Processing stream)
• Returns the current positionwithin the specified le. (limitation? Long data type ).
• Returns-1L on error.
Int rewind (FILE Upload stream)
• Sets the parameter le pointer at thebeginning.
• Equivalent tofseek (stream, 0L, SEEK_SET );
These functions are used more often. Remember to learn them in class. During file stream operations, at least the start position of the file and the position of the current file pointer must be recorded.
<Stdio. h>: file errors
Void clearerr (FILE Upload stream)
• Clears EOF and other errorindicators on stream. Int feof (File character stream)
Int feof (File Processing Stream)
• Return non-zero (true) If end of each le indicator is set for stream.
• Only way to test end of each le forfunctions such as fwrite (), fread ()
Int ferror (file into Stream)
• Returns non-zero (true) If anyerror indicator is set for stream.
<String. h>: memory functions
Void merge memcpy (void merge DST, const void merge SRC, size_t N)
• Copies n Bytes from SRC to location DST
• Returns a pointer to DST.
• SRC and DST cannot overlap.
Void merge memmove (void merge DST, const void merge SRC, size_t N)
• Behaves same as memcpy () function.
• SRC and DST can overlap.
Int memcmp (const void mongocs, const void mongoct, int N)
• Compares into rst n Bytes between CS and CT.
Void merge memset (void merge DST, int C, int N)
• fi lls the specified rst n bytes of dst with the value c.
• Returns a pointer to dst
The biggest advantage of memcpy over memmove is to facilitate parallel processing. These two names are wonderful. copy is copy, and move is move. It is a good programming habit to consider data independence when designing a library.
<Stdlib. h>: Utility
Double atof (const char ∗ s)
Int atoi (const char ∗ s)
Long atol (const char ∗ s)
• Converts character to convert oat, integerand long respectively.
Int rand ()
• Returns a duo-random numbersbetween 0 and RAND_MAX
Void srand (unsigned int seed)
• Sets the seed for thepseudo-random generator!
The previous functions seem to be difficult to implement. That is, the implementation of integer-to-floating-point conversion may be troublesome, and I cannot think of a particularly elegant method.
<Stdlib. h>: exiting
Void abort (void)
• Causes the program to terminateabnormally.
Void exit (int status)
• Causes normal program termination. The value status is returned to the operating system.
• 0 EXIT_SUCCESS indicatessuccessful termination. Any other value indicates failure (EXIT_FAILURE)
Void atexit (void (∗ fcn) (void ))
• Registers a function fcn to becalled when the program terminates normally;
• Returns non zero when registrationcannot be made.
• After exit () is called, the functions are called in reverse order of registration.
Int system (const char cmdcmd)
• Executes the command in string cmd.
• If cmd is not null, the program executes the command and returns exit status returned by the command
The system function is also available in python and is used in many python functions. Why is the execution order of atexit backward.
<Stdlib. h>: search and sort
Void merge bsearch (const void merge key, const void merge base, size_t n, size_t size, int (merge cmp) (const void merge keyval, const void merge datum ));
• Searches base [0] through base [n-1] for * key.
• Function cmp () is used to perform comparison.
• Returns a pointer to the matching item if it exists and NULLotherwise.
Void qsort (void initialize base, size_t n, size_t sz, int (synchronized cmp) (constvoid merge, const void merge ));
• Sorts base [0] through base [n-1] in ascending/descending order.
• Function cmp () is used to perform comparison.
Comparison function pointers in search and sorting are typical examples of using function pointers. Bsearch and qsearch are so common that library functions in many programming languages include them.
<Assert. h>: Diagnostics
Void assert (int expression)
• Used to check for invariants/codeconsistency during debugging
• Does nothing when expression istrue.
• Prints an error message indicating, expression, fi lename and line number.
Alternative ways to print your lename and line number during execution is to use: _ FILE __,__ LINE _ macros.
The assert function should be removed when it is released as release. Now I understand why there are so many assert headers in JUnit.
<Stdarg. h>: Variable Argument lists
Variable argument lists:
• Functions can variable number ofarguments.
• The data type of the argument canbe different for each argument.
• Atleast one mandatory argument isrequired.
• Declaration:
Int printf (char encoded fmt,...);/encoded fmt is last named argument encrypted/
Va_list ap
• Ap de fi nes an iterator that willpoint to the variable argument.
• Before using, it has to beinitialized using va_start.
C, unlike Java, C #, and other explanatory languages running on virtual machines, is fully compiled and run. Therefore, this dynamic feature is implemented by the macro of preprocessing.