Summary of the functions commonly used in the Linux Standard I Dot O Library
The previous article summarized the system calls to the file Istroke O, and today we will summarize the functions that the standard Imax O library has used frequently over the past few years.
The standard iPink O library (# include) actually encapsulates system calls; the main purpose of doing so is to improve efficiency by reducing calls to system calls.
1. Stream and FILE object
The file descriptor for the file I _ FILE O operation, while the standard I _ map O library operates on the stream, that is, the file object. When a stream is opened, a FILE object is returned.
2. Standard input, standard output, standard error
The file descriptor files corresponding to the file I STDIN_FILENO O are: 0 (STDIN_FILENO), 1 (STDOUT_FILENO), 2 (STDERR_FILENO).
The streams corresponding to the standard Icano library are: stdin, stdout, stderr.
3. The standard iCandle O provides three buffers (reducing calls to system calls).
Int setbuf (FILE * stream,char * buf); / * buf = null disable buffer, buf length = BUFSIZE setting buffer * / int setvbuf (FILE * stream,char * buf,int mode,size_t size); / * mode: _ IOFBF full buffer; _ IOLBF line buffer; _ IONBF without buffer. Buf of length size, if buf=NULL, the default size of the system. * / int fflush (FILE*fp); / * Force flush a stream to flush the buffer * / 1) full buffering: full buffering when no interactive device is involved.
2) Row buffering: end devices are generally line buffers. Sometimes you don't need a buffer, as in the previous example, you need to print printf in real time, so set it to no buffer:
Setvbuf (stdout,NULL,_IONBF,0) 3) No buffering: a standard error is without buffering and will be output immediately when an error occurs.
4. Open and close the stream
FILE * fopen (const char * path,const char * mode); FILE * fdopen (int fd,const char * mode); FILE * freopen (const char * path,const char * mode,FILE * stream); all three functions open a stream. Fopen: open a specified stream; fdopen: combine a stream with a file descriptor; freopen: open a specified file on a specified stream, close it if it is opened, or eliminate it if it is directed.
Mode:r read, r + read and write, w write, w + read and write, a write at the end of the file, and a + read and write at the end of the file.
Int fclose (FILE * stream); / * close a stream * /
5. Read and write streams
There are three types of read and write streams: one character at a time, one line at a time, one line at a time, and direct IUnip O (binary Iamp O) operates on bytes.
1) the character Imax O
Read function:
Int getc (FILE * fp); int fgetc (FILE * fp); int getchar (void); / * getc (stdin) * / to determine whether there is an error or whether it has reached the end of the stream (EOF =-1), so the returned values are all integers.
Use the function: int ferror (FILE * fP) / * return 0*//int feof without error (FILE * fp) / * return 0 to indicate that the file is not finished * /
Write function:
Int putc (int cdout * fp); int fputc (int cmeme * fp); int putchar (int c); / * putc (cje stdout) * / 2)
Read function:
Char * fgets (char * buf,int njie file * fp); / * read the next newline character if the buf is big enough, if the buf is not a line large enough, return an incomplete line, and next time read to change the line * / char * gets (char * buf); / * read from stdin * /
Write function:
Char fputs (char * buf,FILE * fp); char puts (char * buf); / * write to stdout*/3) binary Icano
Size_t fread (void*ptr,size_t size,size_t nmemb,FILE * stream); size_t fwite (const void*ptr,size_t size,size_t nmemb,FILE * stream); / * size specifies the ptr length, and nmemb specifies the ptr*/ that reads or writes several size lengths
Often read the image of the device from flash at work:
/ * read boot & image*/cs_status cs_build_image () {cs_uint32 offset = 0; int ret = 0; FILE * bootfp = NULL; FILE * imgfp = NULL; ULONG lByte = 0; / * the unit length when reading the file * / UCHAR ucBuffer [1024] = {0}; / * used to store the read image unit * / ULONG ulImageLen = 0 / * overall length of record file * / UCHAR uc8124imgName [48] = {0}; / * local file name*/ UCHAR uc8124stage2ImgName [48] = {0}; / * local file name*/ UCHAR head = 64; sprintf (uc8124stage2ImgName, "/ ram0/%s", "stage2") If (NULL = (bootfp = fopen (uc8124stage2ImgName, "wb+") {printf ("open to cs8124_img file!\ r\ n"); return ERROR;} ret = GenHwSysFlashload8124Stage2Image (bootfp); if (OK! = ret) {printf ("read 8124Stage2Image from flash error!\ r\ n"); return ERROR } g_uc8124stage2Image = (cs_uint8*) malloc (sizeof (cs_uint8) * TEST_MAX_OLT_LOADER_IMAGE); if (g_uc8124stage2Image = = NULL) {printf ("malloc g_uc8124stage2Image error\ n");} memset (g_uc8124stage2Image, 0, TEST_MAX_OLT_LOADER_IMAGE); fseek (bootfp, 0, SEEK_SET) While (0 < (lByte = fread (ucBuffer, 1, 1024, bootfp) {ulImageLen + = (lByte-head); / * write to local file operations * / memcpy (g_uc8124stage2Image + offset, ucBuffer+head, lByte-head); offset + = lByte-head; memset (ucBuffer, 0, sizeof (ucBuffer)); head = 0 } printf ("[% s% d] g_uc8124stage2Image:%d\ n", _ _ FILE__,__LINE__,ulImageLen); g_ucStage2ImageLen = ulImageLen; if (TEST_MAX_OLT_LOADER_IMAGE < ulImageLen) {return ERROR;} fclose (bootfp); ret = remove (uc8124stage2ImgName); if (OK! = ret) {printf ("8124stage2Image error!\ r\ n"); return ERROR } / * get cs8124 image (firmware) * / sprintf (uc8124imgName, "/ ram0/%s", "imgenew"); if (NULL = (imgfp = fopen (uc8124imgName, "wb+") {printf ("Fail to create cs8124_img file!\ r\ n"); return ERROR;} ret = GenHwSysFlashload8124Image (imgfp) If (OK! = ret) {printf ("read 8124Image from flash error!\ r\ n"); return ERROR;} g_ucpOltImage = (cs_uint8*) malloc (sizeof (cs_uint8) * TEST_MAX_OLT_IMAGE); if (g_ucpOltImage = = NULL) {printf ("malloc g_ucpOltImage error\ n");} memset (g_ucpOltImage, 0, TEST_MAX_OLT_IMAGE) Fseek (imgfp, 0, SEEK_SET); lByte = 0; ulImageLen = 0; offset = 0; head = 64; memset (ucBuffer, 0, sizeof (ucBuffer)); while (0 < (lByte = fread (ucBuffer, 1, 1024, imgfp) {ulImageLen + = (lByte-head); / * write to local file operations * / memcpy (g_ucpOltImage + offset, ucBuffer+head, lByte-head) Offset + = lByte-head; memset (ucBuffer, 0, sizeof (ucBuffer)); head = 0;} printf ("[% s% d] g_ucpOltImage len:%d\ n", _ _ FILE__,__LINE__,ulImageLen); g_ucOltImageLen = ulImageLen; if (TEST_MAX_OLT_IMAGE < ulImageLen) {return ERROR;} fclose (imgfp); ret = remove (uc8124imgName) If (OK! = ret) {printf ("uc8124imgName error!\ r\ n"); return ERROR;}} 6, location stream function
Int fseek (FILE * fp,long offset,int whence); / * is similar to the file I/Olseek function, whence is the same: SEEK_SET starts from the beginning of the file, SEEK_CUR starts from the current file, SEEK_END starts from the end of the file * / 7, formatting
Format the output function:
Int printf (const char * format, … Int fprintf (FILE * fp,const char * format, … Int sprintf (char * buf,const char * format, …) ; int snprintf (char * buf,size_t NJ Const char * format, …) / * printf writes formatted data to standard output, fprintf writes to the specified stream, and sprintf sends formatted strings to the array buf. Snprintf specifies the length of buf /
Snprintf is often used at work, for example, static routing has just been developed:
Format the input function (similar to the output):
Int scanf (const * format, …) ; int fscanf (FILE * fp,const * format,..); int sscanf (const char * buf,const char * format, …) 8. Create a temporary file function
FILE * tmpfile (void); / * create a temporary binary file wb+, will automatically delete the file when it is closed or when the program ends * /