Diagnostic functions, in the function libraries assert.h and math.h
void assert(int test) a macro expanded much like an if statement; if the test fails, it displays a message and terminates the program abnormally, no return value
void perror(char *string) this function displays the most recent error message, in the format: string string:error message
char *strerror(char *str) this function returns the most recent error message, in the format: string str:error message
int matherr(struct exception *e)
user-modified math error return message function (not necessary to use)
double _matherr(_mexcep why,char *fun,double *arg1p, double *arg2p,double retval)
user-modified math error return message function (not necessary to use)
Input/output subroutines, function libraries are io.h, conio.h, stat.h, dos.h, stdio.h, signal.h
int kbhit() this function returns the most recently pressed key
int fgetchar() reads one character from the console (keyboard), displaying it on the screen
int getch() reads one character from the console (keyboard), without displaying it on the screen
int putch() writes one character to the console (keyboard)
int getchar() reads one character from the console (keyboard), displaying it on the screen
int putchar() writes one character to the console (keyboard)
int getche() reads one character from the console (keyboard), displaying it on the screen
int ungetch(int c) pushes character c back to the console (keyboard)
char *cgets(char *string) reads a string from the console (keyboard) and stores it in string
int scanf(char *format)
reads a string from the console, assigns values to each parameter, and uses BIOS for output
int vscanf(char *format,Valist param)
reads a string from the console, assigns values to each parameter, and uses BIOS for output, parameters are taken from Valist param
int cscanf(char *format)
reads a string from the console, assigns values to each parameter, and operates directly on the console, for example when the display is showing characters it uses direct video memory writing for display
int sscanf(char *string,char *format)
assigns values to each parameter through string string
int vsscanf(char *string,char *format,Vlist param)
assigns values to each parameter through string string, parameters are taken from Vlist param
int puts(char *string) sends a string string to the console (display), using BIOS for output
void cputs(char *string) sends a string string to the console (display), operating directly on the console, for example the display uses direct video memory writing for display
int printf(char *format)
sends formatted string output to the console (display), using BIOS for output
int vprintf(char *format,Valist param)
sends formatted string output to the console (display), using BIOS for output, parameters are taken from Valist param
int cprintf(char *format)
sends formatted string output to the console (display), operating directly on the console, for example the display uses direct video memory writing for display
int vcprintf(char *format,Valist param)
sends formatted string output to the console (display), operating directly on the console, for example the display uses direct video memory writing for display, parameters are taken from Valist param
int sprintf(char *string,char *format)
replaces the contents of string string with the formatted string
int vsprintf(char *string,char *format,Valist param)
replaces the contents of string string with the formatted string, parameters are taken from Valist param
int rename(char *oldname,char *newname)changes the name of file oldname to newname
int ioctl(int handle,int cmd)
this function is used to control input/output devices, see the table below:
┌───┬────────────────────────────┐
│cmd value │function │
├───┼────────────────────────────┤
│ 0 │get device information │
│ 1 │set device information │
│ 2 │read argcx bytes into the address pointed to by argdx │
│ 3 │write argcx bytes to the address pointed to by argdx │
│ 4 │same as cmd=2 except handle is treated as a device number (0=current,1=A, etc.) │
│ 5 │same as cmd=3 except handle is treated as a device number (0=current,1=A, etc.) │
│ 6 │get input status │
│ 7 │get output status │
│ 8 │test removability; DOS 3.x only │
│ 11 │set retry count for share conflicts; DOS 3.x only │
└───┴────────────────────────────┘
int (*ssignal(int sig,int(*action)())() executes a software signal (not necessary to use)
int gsignal(int sig) executes a software signal (not necessary to use)
int _open(char *pathname,int access)opens a file for reading or writing, and uses access to determine whether it is opened for reading or writing, see the table below for access values
┌──────┬────────────────────┐
│access value │meaning │
├──────┼────────────────────┤
│O_RDONLY │read file │
│O_WRONLY │write file │
│O_RDWR │read and write │
│O_NOINHERIT │included if the file is not passed to the subprogram │
│O_DENYALL │only allow the current process to access the file │
│O_DENYWRITE │only allow reading from any other opened file │
│O_DENYREAD │only allow writing from any other opened file │
│O_DENYNONE │allow other shared opens of the file │
└──────┴────────────────────┘
int open(char *pathname,int access)opens a file for reading or writing, and uses access to determine whether it is opened for reading or writing, see the table below for access values
┌────┬────────────────────┐
│access value│meaning │
├────┼────────────────────┤
│O_RDONLY│read file │
│O_WRONLY│write file │
│O_RDWR │read and write │
│O_NDELAY│unused; for UNIX system compatibility │
│O_APPEND│read and write, but each write always appends at the end of the file │
│O_CREAT │if the file exists, this flag is useless; if it does not exist, create a new file │
│O_TRUNC │if the file exists, its length is truncated to 0, attributes unchanged │
│O_EXCL │unused; for UNIX system compatibility │
│O_BINARY│this flag can explicitly specify opening the file in binary mode │
│O_TEXT │this flag can be used to explicitly specify opening the file in text mode│
└────┴────────────────────┘
permiss is the file attribute, and can be one of the following values:
S_IWRITE allows writing S_IREAD allows reading S_IREAD|S_IWRITE allows reading and writing
int creat(char *filename,int permiss) creates a new file filename and sets its readability/writability.
permiss is the file readability/writability, and can be one of the following values
S_IWRITE allows writing S_IREAD allows reading S_IREAD|S_IWRITE allows reading and writing
int _creat(char *filename,int attrib) creates a new file filename and sets file attributes.
attrib is the file attribute, and can be one of the following values
FA_RDONLY read-only FA_HIDDEN hidden FA_SYSTEM system
int creatnew(char *filenamt,int attrib) creates a new file filename and sets file attributes.
attrib is the file attribute, and can be one of the following values
FA_RDONLY read-only FA_HIDDEN hidden FA_SYSTEM system
int creattemp(char *filenamt,int attrib) creates a new file filename and sets file attributes.
attrib is the file attribute, and can be one of the following values
FA_RDONLY read-only FA_HIDDEN hidden FA_SYSTEM system
int read(int handle,void *buf,int nbyte) reads nbyte characters from the file with file number handle into buf
int _read(int handle,void *buf,int nbyte) reads nbyte characters from the file with file number handle into buf, directly calling MSDOS to operate.
int write(int handle,void *buf,int nbyte) writes nbyte characters from buf into the file with file number handle
int _write(int handle,void *buf,int nbyte) writes nbyte characters from buf into the file with file number handle
int dup(int handle) duplicates a file handle handle and returns this handle
int dup2(int handle,int newhandle) duplicates file handle handle to newhandle
int eof(int *handle) checks whether the file has ended, returns 1 if ended, otherwise returns 0
long filelength(int handle) returns the file length, handle is the file number
int setmode(int handle,unsigned mode)this function is used to set the opening mode of the file with file number handle
int getftime(int handle,struct ftime *ftime)
reads the time of the file with file number handle, and stores the file time in structure ftime, returns 0 on success, ftime structure is as follows:
┌─────────────────┐
│struct ftime │
│{ │
│ unsigned ft_tsec:5; /*seconds*/ │
│ unsigned ft_min:6; /*minutes*/ │
│ unsigned ft_hour:5; /*hours*/ │
│ unsigned ft_day:5; /*day*/ │
│ unsigned ft_month:4;/*month*/ │
│ unsigned ft_year:1; /*year-1980*/ │
│} │
└─────────────────┘
int setftime(int handle,struct ftime *ftime) rewrites the file time of the file with file number handle,
the new time is in structure ftime. Returns 0 on success. The structure ftime is as follows:
┌─────────────────┐
│struct ftime │
│{ │
│ unsigned ft_tsec:5; /*seconds*/ │
│ unsigned ft_min:6; /*minutes*/ │
│ unsigned ft_hour:5; /*hours*/ │
│ unsigned ft_day:5; /*day*/ │
│ unsigned ft_month:4;/*month*/ │
│ unsigned ft_year:1; /*year-1980*/ │
│} │
└─────────────────┘
long lseek(int handle,long offset,int fromwhere)
this function moves the pointer of the file with file number handle to the offset-th byte after fromwhere.
SEEK_SET file switch SEEK_CUR current position SEEK_END end of file
long tell(int handle) this function returns the file pointer of the file with file number handle, in bytes
int isatty(int handle)this function is used to get the type of device handle
int lock(int handle,long offset,long length) locks file sharing
int unlock(int handle,long offset,long length) unlocks file sharing
int close(int handle) closes the file handle represented by handle, handle is a file handle obtained from one of _creat, creat,
creatnew, creattemp, dup, dup2, _open, open
returns 0 on success, otherwise returns -1, usable on UNIX systems
int _close(int handle) closes the file handle represented by handle, handle is a file handle obtained from one of _creat, creat,
creatnew, creattemp, dup, dup2, _open, open
returns 0 on success, otherwise returns -1, usable only on MSDOS systems
FILE *fopen(char *filename,char *type) opens a file filename, with opening mode type,
and returns this file pointer, type can be one of the following strings plus a suffix
┌──┬────┬───────┬────────┐
│type│read/write │text/binary file│create new/open old file │
├──┼────┼───────┼────────┤
│r │read │text │open old file │
│w │write │text │create new file │
│a │append │text │open if it exists, otherwise create new│
│r+ │read/write │unrestricted │open │
│w+ │read/write │unrestricted │create new file │
│a+ │read/append │unrestricted │open if it exists, otherwise create new│
└──┴────┴───────┴────────┘
the suffixes that can be added are t and b. Adding b means the file is operated in binary form; t is not necessary to use
Example: ┌──────────────────┐
│#include │
│main() │
│{ │
│ FILE *fp; │
│ fp=fopen("C:\\WPS\\WPS.EXE","r+b"

;│
└──────────────────┘
FILE *fdopen(int ahndle,char *type)
FILE *freopen(char *filename,char *type,FILE *stream)
int getc(FILE *stream) reads one character from stream stream, and returns that character
int putc(int ch,FILE *stream) writes one character ch to stream stream
int getw(FILE *stream) reads an integer from stream stream, returns EOF on error
int putw(int w,FILE *stream) writes an integer to stream stream
int ungetc(char c,FILE *stream) pushes character c back to stream stream, the next character read will be c
int fgetc(FILE *stream) reads one character from stream stream, and returns that character
int fputc(int ch,FILE *stream) writes character ch into stream stream
char *fgets(char *string,int n,FILE *stream)
reads n characters from stream stream into string
int fputs(char *string,FILE *stream)writes string string into stream stream
int fread(void *ptr,int size,int nitems,FILE *stream)
reads nitems strings of length size from stream stream into ptr
int fwrite(void *ptr,int size,int nitems,FILE *stream)
writes nitems strings of length size to stream stream, the strings are in ptr
int fscanf(FILE *stream,char *format)
reads a string from stream stream in formatted form
int vfscanf(FILE *stream,char *format,Valist param)
reads a string from stream stream in formatted form, parameters are taken from Valist param
int fprintf(FILE *stream,char *format)
writes a string to the specified stream stream in formatted form
int vfprintf(FILE *stream,char *format,Valist param)
writes a string to the specified stream stream in formatted form, parameters are taken from Valist param
int fseek(FILE *stream,long offset,int fromwhere)
the function moves the file pointer to the offset-th byte after the position pointed to by fromwhere, fromwhere can be one of the following values:
SEEK_SET file switch SEEK_CUR current position SEEK_END end of file
long ftell(FILE *stream) the function returns the current file pointer position in stream, in bytes
int rewind(FILE *stream) moves the current file pointer stream to the beginning of the file
int feof(FILE *stream) checks whether the file pointer on stream stream is at the end position
int fileno(FILE *stream) gets the file handle on stream stream and returns the file handle
int ferror(FILE *stream) checks whether there is a read/write error on stream stream, returns 1 if there is an error
void clearerr(FILE *stream) clears read/write errors on stream stream
void setbuf(FILE *stream,char *buf) assigns buffer buf to stream stream
void setvbuf(FILE *stream,char *buf,int type,unsigned size)
assigns buffer buf to stream stream, size is size, type is type, see the table below for type values
┌───┬───────────────────────────────┐
│type value│meaning │
├───┼───────────────────────────────┤
│_IOFBF│the file is fully buffered; when the buffer is empty, the next input operation will try to fill the entire buffer│
│ │. On output, the buffer will be completely filled before any data is written to the file. │
│_IOLBF│the file is line buffered. When the buffer is empty, the next input operation will still try to fill the entire buf│
│ │fer. However, on output, whenever a newline character is written to the file, the buffer is flushed. │
│_IONBF│the file is unbuffered. The buf and size parameters are ignored. Each input operation reads directly from the fi│
│ │le, and each output operation immediately writes data to the file. │
└───┴───────────────────────────────┘
int fclose(FILE *stream) closes a stream, which may be a file or a device (for example LPT1)
int fcloseall() closes all streams except stdin or stdout
int fflush(FILE *stream)
closes a stream, and processes the buffer: for a read stream, reads the contents of the stream into the buffer; for a write stream, writes the contents of the buffer into the stream. Returns 0 on success
int fflushall()
closes all streams, and processes each stream's buffer: for a read stream, reads the contents of the stream into the buffer; for a write stream, writes the contents of the buffer into the stream. Returns 0 on success
int access(char *filename,int amode)
this function checks file filename and returns the file's attributes, the function stores the attributes in amode, amode is composed of the following bit combinations
06 readable and writable 04 readable 02 writable 01 executable (ignored) 00 file exists
if filename is a directory, the function only determines whether the directory exists; the function returns 0 on success, otherwise returns -1
int chmod(char *filename,int permiss) this function is used to set the attributes of file filename
permiss can be one of the following values
S_IWRITE allows writing S_IREAD allows reading S_IREAD|S_IWRITE allows reading and writing
int _chmod(char *filename,int func);
this function is used to read or set the attributes of file filename,
when func=0, the function returns the attributes of the file; when func=1, the function sets the attributes of the file
if setting file attributes, attrib can be one of the following constants
FA_RDONLY read-only FA_HIDDEN hidden FA_SYSTEM system