Linux Standard I/O (ii)

Source: Internet
Author: User
Tags fread int size sprintf stdin

<uinx Environment Advanced Programming Note > I used to encounter two I/O operations such as the F-fopen, Fread, Fwrite class is no F-first open, read, fwrite originally a Unix I/O (implemented on UNIX-like systems), Another class is standard I/O (implemented on many systems, including windows)  

Benefits of using standard I/O

Because this library is implemented on many UNIX operating systems (including Windows, Linux), all facilitates the porting of software. The main difference from UNIXI/OUNIX I/O functions are for file descriptors, and standard I/O operations revolve around streams. The flow is a file * standard I/O provides caching-to minimize write, read calls. Standard I/O is less efficient because it is more of a layer of encapsulation. That is, Fread is implemented by calling read. header file for standard I/OStdio.h three standard I/O flow pre-defined pointers: stdin, stdout, stderr (Unix I/o: Stdin_fileno, Stdout_fileno, Stderr_fileno) CacheThere is a delay in having the cache, that is, the content on the output device and the content in the cache are likely to be different. You can call Fflush to flush the cache. There are several types of caches that can be called to change the default cache type by calling the following API. Setbuf, Setvbufsetvbuf can accurately describe the type of cache. Fclose The stream is also flushed when the stream is closed. When a process Normal termination (call exit directly or return from the main function), all standard I/O streams with non-writable cache data are flushed and all open standard I/O streams are closed. standard I/O API Open streamFILE * fopen (const char * path,const char * mode); The mode parameter is simple: R files are read-only r+ files can be read-write, files must exist W file only write. Equivalent to deleting the original file and creating a new file. The even if file exists, and the length becomes 0. If the file does not exist, it is created. w+ files can be read and written. The other is the same as W. A write-only file opens in an additional way. The file does not exist and is created automatically. A + opens read-write files in an additional way. The file does not exist and is created automatically. b manipulate the file in binary mode. Can be combined with any one of the above. Automatically created file access permission bit: 644 read/write stream I/O for one character at a timeGetc, fgetc, GETCHARGETC is generally called macro, efficiency is higher 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), and after reading one character from a stream, you can call ungetc to send the character back to the reflow. Above three API error or reach end of file return-1. In particular, it is necessary to invoke the following two APIs to determine: int ferror (file *stream); int feof (file *stream); under what circumstances will the loopback character be used?The loopback character operation is often used when an input stream is being read and some form of word or tick operation is performed. Sometimes you need to look at the next character to decide how to handle the current character. You then need to conveniently return the characters you just viewed so that the character is returned the next time you call G e t C. Corresponding output: PUTC, FPUTC, Putchar I/O for each lineGets, Fgetschar * fgets (char * s,int size,file * stream), Char *gets (char *s), fgets read from file, get read from table. Fgets () is used to read the characters from the file referred to in the parameter stream to the memory space referred to in the argument s, until a newline character is present, the end of the file is read, or the size-1 character is read, and then NULL is terminated as a string. Line breaks are also included in the string. Remove newline characters: S[strlen (s) -1]=0;gets automatically deletes new line characters. It is recommended that you do not use get because of the risk of buffer overflow. Corresponding output: Fputs will not output a new line character after puts and will output a new line character Specify the length of I/OThe above API is primarily used to work with text files, and it is not easy to handle the case of a struct-type content or structure that contains a null character or line break. This will use the following two APIs. 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,fi Two functions return the number of objects actually read and written. Locating a streamLong Ftell (file * stream), int fseek (file * stream,long offset,int whence), void rewind (file * stream); Equivalent to Fseek (stream, 0, Seek_set), the following two portability 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 to the specified stream, sprintf the formatted character into the array buf. sprintf automatically adds a null byte to the end of the array, but the 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,.......); The 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 for 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), which is required if you want to invoke functions such as DUP or FCNTL. Create a temporary fileFILE *tmpfile (void)--a char *tmpnam (const char *) available under Linux--cannot be used under Linux char *tempnam (const char *dir, const char *prefix); --Not available under Linux char * mktemp (char * template); --Linux The last six characters in the file name string referred to in the available parameters template must be XXXXXX. An example of Linux: Char template[]= "aaaaa-xxxxxx"; Mktemp (template), or mkdtemp (template);

Linux Standard I/O (ii)

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.