Previous summed up the file I / O system calls today to summarize function standard I / O libraries often use at work a few years.
Standard I / O library ( #include <stdio.h> ) is actually encapsulates the system call; main purpose of this is to reduce the system call calls, thereby improving efficiency.
1, flow and FILE objects
File I / O operations of the file descriptor, the standard I / O library is a streaming operation is i.e. FILE object. When opening a stream, it returns a FILE object.
2, the standard input, standard output and standard error
Corresponding to the file I / O file descriptor file: 0 ( STDIN_FILENO ), . 1 ( STDOUT_FILENO ), 2 ( STDERR_FILENO )
Corresponds to a standard I / O library stream: stdin , stdout , stderr ;
3, the standard I / O provides three buffers (reduction of calls to the system call)
int setbuf (FILE * stream, char * buf); / * buf = null closed buffer, buf = BUFSIZE set the buffer length * / int the setvbuf (Stream the FILE *, char * buf, int MODE, size_t size); / * MODE : _IOFBF fully buffered; _IOLBF line buffer; _IONBF unbuffered. Length size of buf, if buf = NULL, the default size for the system. * / Int fflush (the FILE * FP); / * Forced refresh a stream buffer used to refresh * /
1) Full Buffer: when not related to an interactive device, the buffer was full.
2) Line Buffer: line terminal devices are generally buffered. Sometimes do not need a buffer, such as the previous example, the need for real-time printing printf , so talk about it without a buffer set:
setvbuf(stdout,NULL,_IONBF,0)
3) No cushion: standard error is unbuffered, when an error occurs will immediately output.
4, opening and closing the flow
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);
Three functions are a flow opening, the fopen : open a specified stream; fdopen : stream and a combined file descriptor; The freopen : open a specified file on a specified stream, is turned off, the elimination of the oriented .
mode: r Read, R & lt + read, W write, W + write, A at the end of the file write, A + in the end of the file read and write.
int fclose (FILE * stream); / * Close a stream * /
5, reading and writing flow
Read and write stream into three types: one character of the I / O ; each line of the I / O ; directly the I / O (Binary the I / O ) operations byte.
1) Character I / O
Read function:
int getc(FILE *fp); int fgetc(FILE *fp); int getchar(void);/*getc(stdin)*/
Due to the error, or is not determined to the end of the stream ( the EOF = -1 ), so the return value is an integer.
By function: int ferror (FILE * fP) / * No error return 0 * // int feof (FILE * fp) / * return 0 indicates that the document has not been completed * /
Write function:
int putc(int c,FILE *fp); int fputc(int c,FILE *fp); int putchar(int c);/*putc(c,stdout)*/
2) line I / O
Read function:
char * fgets (char * buf, int n, FILE * fp); / * read the next line feed buf if large enough, if there is no large buf line, a return line is not complete, the next to be diverted read * / char * gets (char * buf); / * read from stdin * /
Write function:
char fputs(char *buf,FILE *fp); char puts(char *buf);/*写到stdout*/
3) the binary I / O
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 specified ptr length, of nmemb specify read or write several size length ptr * /
Working often from flash reading apparatus mirror:
/ * Read Boot Image * & / cs_status cs_build_image () { cs_uint32 offset = 0; int RET = 0; the FILE * bootfp = NULL; the FILE * imgfp = NULL; ULONG lByte = 0; / * file reading unit length * / UCHAR ucBuffer [1024] = {0}; / * means for storing the image read out * / ULONG ulImageLen = 0; / * total length of the 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 the 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); /*写在本地文件操作*/ 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); /*写在本地文件操作*/ 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, the positioning stream function
int fseek (FILE * fp, long offset, int whence); similar / * and file I / Olseek function, the same whence: SEEK_SET file starting from the start, the current file from the beginning SEEK_CUR, SEEK_END starting end of file * /
7, formatted I / O
Output formatting functions:
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 n,const char *format,…);
/ * printf the formatted data is written to standard output, fprintf written to the specified stream, sprintf formatted string to the array buf in. snprintf specified buf length n * /
Often use at work snprintf , for example, has just finished developing a static route is used:
Formatted input function (output and similar):
int scanf(const *format,…); int fscanf(FILE *fp,const *format,..); int sscanf(const char *buf,const char *format,…);
8, create a temporary file functions
FILE * tmpfile (void); / * Create a temporary binary file wb +, this file will be automatically deleted at the end of the program or close the file * /