I/O standard library

Source: Internet
Author: User
Tags fread rewind
<Uinx environment advanced programming notes> in the past, we often encountered two types of I/O operations: fopen, fread, and fwrite with f headers, and open, read without f headers, fwrite is originally a unix I/O (implemented on Unix-like systems) and a standard I/O (implemented on many systems, including windows)  

Benefits of using standard I/O

Because this library is implemented in many UNIX operating systems (including Windows and linux), it is helpful for software porting. Main differences with UnixI/OUnix I/O functions are all for file descriptors, and standard I/O operations are performed around the stream. The stream is a FILE * standard I/O that provides cache-to minimize write and read calls. Standard I/O is less efficient, because it has an additional encapsulation layer. That is, fread is implemented by calling read. Standard I/O header filesStdio. h: stdin, stdout, stderr (Unix I/O: STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO) CacheIf there is a cache, there will be latency, that is, the content on the output device may be different from the content in the cache. You can call fflush to refresh the cache. There are multiple cache types. You can call the following API to change the default cache type. Setbuf and setvbufsetvbuf can precisely describe the cache type. When fclose closes the stream, it also refreshes the stream. When a process Normal termination (direct call of exit or return from main function), All standard I/O streams with unwritten cache data will be refreshed, and all opened standard I/O streams will be closed. Standard I/O API Open streamFILE * fopen (const char * path, const char * mode); mode parameter is simple: r FILE Read-Only r + FILE can be read and written, FILE must exist w FILE write only. This is equivalent to deleting the original file and creating a new file. That is, if the file exists, the length is changed to 0. If the file does not exist, it is created. W + files can be read and written. Others are the same as w. A. Open and write-only files as an attachment. The file does not exist and is automatically created. A + opens the read/write file as an attachment. The file does not exist and is automatically created. B. Operate the file in binary mode. Can be combined with any of the above. The automatic file access limit is 644. Read/write stream I/O of one character each timeGetc, fgetc, and getchargetc are generally macro calls with higher efficiency than fgetc. Fgetc can be passed as an address to other functions. Int getc (FILE * stream); int fgetc (FILE * stream); int getchar (void) is equivalent to getc (stdin); after reading a character from a stream, you can call ungetc to send the character back. -1 is returned if an error occurs in the preceding three APIs or the end of the file is reached. The following two APIs need to be called to determine the specific situation: int ferror (FILE * stream); int feof (FILE * stream ); Under what circumstances will the return character be used?When you are reading an input stream and performing some form of word or mark-breaking operations, the return character operation is often used. Sometimes you need to take a look at the next character to determine how to process the current character. Then, you need to conveniently return the character you just viewed so that this character can be returned the next time you call g e t c. Output: putc, fputc, putchar I/O of each rowGets, fgetschar * fgets (char * s, int size, FILE * stream); char * gets (char * s); fgets reads from the FILE, and gets reads from the input in the table. Fgets () is used to read characters from the file referred to by the parameter stream to the memory space referred to by the parameter s until a line break occurs, the file is read to the end, or the size-1 character is read, finally, NULL is added as the string. Linefeeds are also contained in strings. Remove line breaks: s [strlen (s)-1] = 0; gets automatically deletes new line breaks. We recommend that you do not use gets because it is easy to cause buffer overflow. Corresponding output: After fputs, a new line character will not be output after puts. Specify the length of I/OThe above API is mainly used to process text files. It is difficult to handle situations where the structure content or structure contains null characters or line breaks. The following two APIs are used. Size_t fread (void * ptr, size_t size, size_t nmemb, FILE * stream); size_t fwrite (const void * ptr, size_t size, size_t nmemb, FILE * stream ); both functions return the actual number of Read and Write objects. Locate streamLong ftell (FILE * stream); int fseek (FILE * stream, long offset, int whence); void rewind (FILE * stream); equivalent to fseek (stream, 0, SEEK_SET ); the portability of the following two items will be better: int fgetpos (FILE *, fpos_t *); int fsetpos (FILE *, const fpos_t *); Format I/OPrintf writes the formatted data to the standard output, fprintf writes the data to the specified stream, and sprintf sends the formatted characters to the array buf. Sprintf automatically adds a null byte to the end of the array, but this byte is not included in the return value. Int fprintf (FILE * stream, const char * format ,.......); int sprintf (char * str, const char * format ,.........); int printf (const char * format ,.......); corresponding three formatted input int fscanf (FILE * stream, const char * format ,.......); int sscanf (char * str, const char * format ,.........); int scanf (const char * format ,.......); Implementation Details of standard I/OOn UNIX, standard I/O is implemented by calling unix I/O, similar to fread (){...... Read ()......} Each stream has a corresponding FILE descriptor that can be obtained by calling int fileno (FILE * fp). If you want to call functions such as dup or fcntl, you need to use this conversion. Create temporary filesFILE * tmpfile (void) -- char * tmpnam (const char *) is available in linux -- char * tempnam (const char * dir, const char * prefix) cannot be used in linux ); -- char * mktemp (char * template) is unavailable in linux. -- the last six characters in the file name string specified by the template parameter in linux must be XXXXXX. An example in linux: char template [] = "aaaaa-XXXXXX"; mktemp (template); or mkdtemp (template );

In the past, we often encountered two types of I/O operations: fopen, fread, and fwrite with f headers, and open, read without f headers, fwrite is originally a unix I/O (implemented on Unix-like systems) and a standard I/O (implemented on many systems, including windows)  

Benefits of using standard I/O

Because this library is implemented in many UNIX operating systems (including Windows and linux), it is helpful for software porting. Main differences with UnixI/OUnix I/O functions are all for file descriptors, and standard I/O operations are performed around the stream. The stream is a FILE * standard I/O that provides cache-to minimize write and read calls. Standard I/O is less efficient, because it has an additional encapsulation layer. That is, fread is implemented by calling read. Standard I/O header filesStdio. h: stdin, stdout, stderr (Unix I/O: STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO) CacheIf there is a cache, there will be latency, that is, the content on the output device may be different from the content in the cache. You can call fflush to refresh the cache. There are multiple cache types. You can call the following API to change the default cache type. Setbuf and setvbufsetvbuf can precisely describe the cache type. When fclose closes the stream, it also refreshes the stream. When a process Normal termination (direct call of exit or return from main function), All standard I/O streams with unwritten cache data will be refreshed, and all opened standard I/O streams will be closed. Standard I/O API Open streamFILE * fopen (const char * path, const char * mode); mode parameter is simple: r FILE Read-Only r + FILE can be read and written, FILE must exist w FILE write only. This is equivalent to deleting the original file and creating a new file. That is, if the file exists, the length is changed to 0. If the file does not exist, it is created. W + files can be read and written. Others are the same as w. A. Open and write-only files as an attachment. The file does not exist and is automatically created. A + opens the read/write file as an attachment. The file does not exist and is automatically created. B. Operate the file in binary mode. Can be combined with any of the above. The automatic file access limit is 644. Read/write stream I/O of one character each timeGetc, fgetc, and getchargetc are generally macro calls with higher efficiency than fgetc. Fgetc can be passed as an address to other functions. Int getc (FILE * stream); int fgetc (FILE * stream); int getchar (void) is equivalent to getc (stdin); after reading a character from a stream, you can call ungetc to send the character back. -1 is returned if an error occurs in the preceding three APIs or the end of the file is reached. The following two APIs need to be called to determine the specific situation: int ferror (FILE * stream); int feof (FILE * stream ); Under what circumstances will the return character be used?When you are reading an input stream and performing some form of word or mark-breaking operations, the return character operation is often used. Sometimes you need to take a look at the next character to determine how to process the current character. Then, you need to conveniently return the character you just viewed so that this character can be returned the next time you call g e t c. Output: putc, fputc, putchar I/O of each rowGets, fgetschar * fgets (char * s, int size, FILE * stream); char * gets (char * s); fgets reads from the FILE, and gets reads from the input in the table. Fgets () is used to read characters from the file referred to by the parameter stream to the memory space referred to by the parameter s until a line break occurs, the file is read to the end, or the size-1 character is read, finally, NULL is added as the string. Linefeeds are also contained in strings. Remove line breaks: s [strlen (s)-1] = 0; gets automatically deletes new line breaks. We recommend that you do not use gets because it is easy to cause buffer overflow. Corresponding output: After fputs, a new line character will not be output after puts. Specify the length of I/OThe above API is mainly used to process text files. It is difficult to handle situations where the structure content or structure contains null characters or line breaks. The following two APIs are used. Size_t fread (void * ptr, size_t size, size_t nmemb, FILE * stream); size_t fwrite (const void * ptr, size_t size, size_t nmemb, FILE * stream ); both functions return the actual number of Read and Write objects. Locate streamLong ftell (FILE * stream); int fseek (FILE * stream, long offset, int whence); void rewind (FILE * stream); equivalent to fseek (stream, 0, SEEK_SET ); the portability of the following two items will be better: int fgetpos (FILE *, fpos_t *); int fsetpos (FILE *, const fpos_t *); Format I/OPrintf writes the formatted data to the standard output, fprintf writes the data to the specified stream, and sprintf sends the formatted characters to the array buf. Sprintf automatically adds a null byte to the end of the array, but this byte is not included in the return value. Int fprintf (FILE * stream, const char * format ,.......); int sprintf (char * str, const char * format ,.........); int printf (const char * format ,.......); corresponding three formatted input int fscanf (FILE * stream, const char * format ,.......); int sscanf (char * str, const char * format ,.........); int scanf (const char * format ,.......); Implementation Details of standard I/OOn UNIX, standard I/O is implemented by calling unix I/O, similar to fread (){...... Read ()......} Each stream has a corresponding FILE descriptor that can be obtained by calling int fileno (FILE * fp). If you want to call functions such as dup or fcntl, you need to use this conversion. Create temporary filesFILE * tmpfile (void) -- char * tmpnam (const char *) is available in linux -- char * tempnam (const char * dir, const char * prefix) cannot be used in linux ); -- char * mktemp (char * template) is unavailable in linux. -- the last six characters in the file name string specified by the template parameter in linux must be XXXXXX. An example in linux: char template [] = "aaaaa-XXXXXX"; mktemp (template); or mkdtemp (template );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.