C Programming language Note (15) Reference manual 7

Source: Internet
Author: User
Tags sprintf stdin

Standard library
The standard library of ANSI-defined standard libraries is not a component of the C language itself, but the implementation of the standard C support provides the function declarations, types, and function types in the library, as well as the macros defined in the standard header file in the following:<assert.h>< ctype.h><errno.h><float.h><limits.h><locale.h><math.h><setjmp.h>< Signal.h><stdarg.h><stddef.h><stdio.h><stdlib.h><string.h><time.h> Header files can be accessed in the following ways: # include < header files > header files are included in any order, And can contain any number of header files that must be contained outside any external declaration or definition must be included in the header file before any declarations that contain header files are not necessarily the source files the external identifiers that begin with the underscore are reserved for use by the standard library

  

input/Output <stdio.h>

The number of input and output functions, types, and macros defined in header file <stdio.h> is almost one-third of the total standard library source or destination of data associated with a disk or other peripheral device although in some systems (such as UNIX), the text stream and the binary stream are the same, But standard Kuzhingan provides these two types of stream text streams that are sequences of lines of text, each containing 0 or more characters and ending with ' \ n ' as a sequence of unprocessed bytes that record internal data if a binary stream is written to the same system, and then the binary stream is read, Read and write exactly the same. Open a stream that will connect the stream to a file or device, close the stream and open a file will return a pointer to an object of type file that records all the necessary information to control the flow when the program starts executing, stdin stdout and stderr These 3 streams are already in the open state

file Operations

File *fopen (const char *filename,const char *mode); the fopen function opens the filename specified by the files and returns a stream associated with it if the open fails, the null access mode is returned: "R"  reads "W "Write and delete existing content" a "  append" r+ "  Open Text file for update" w+ "  Create text for update" A + "to  Open or create file to update, append to file when writing file to end

File *freopen (const char *filename,const char *mode,file *stream); opens filename specified by mode. and associates the file to stream specified stream successfully returned stream, error returns null The Freopen function is typically used to change the file int fflush (Files *stream) associated with stdin stdout stderr, and for the output stream, The Fflush function writes all data that has been written to the buffer but has not yet been written to the file for the input stream, and the result is undefined if the method is incorrect during the write process, or return EOF otherwise 0fflush (NULL)  will clean all output streams int flose (FILE * Stream), writes all non-writable data to the stream, discards all unread input data for the buffer, releases all automatically allocated buffers, and finally closes the stream if an error returns EOF, returns 0

  

int remove (const char *filename) deletes the specified file and returns a non-0 value if the deletion fails. int rename (const char *oldname,const char *newname), modify the file name, Failed to return non-0;file *tmpfile (void); Tmpfile function creates a temporary file in the mode "wb+" that automatically deletes char *tmenam (char S[l_tmpname] when it is closed or the program ends normally); Tmpnam ( NULL) function creates a string that differs from the existing file name and returns a pointer to an internal static array int setvbuf (file *stream,char *buf,int mode, size_t size); The SETVBUF function controls the buffering of the stream stream. You must call this function void Setbuf (FILE *stream,char *buf) before performing read-write and any other operations; If the BUF value is null, the buffer of the stream is closed

  

formatted output

The printf function provides a formatted output conversion of int fprintf (FILE *stream,const char *format,...); The fprintf function transforms the output in the format described by the format and writes it to the stream. Returns the number of characters actually written, the error returns a negative value int sprintf (char *s,const char *format,...); The sprintf function is basically the same as the printf function, but its output is written to the string s, and the vprintfvfprintfvsprintf function is equivalent to the function of printf, respectively.

  

format Input

The SCANF function handles the formatted input transformation int fscanf (FILE *stream,const char *format,...); The FSCANF function reads input from the stream stream according to format string, and assigns the converted value to subsequent parameters, where each parameter must be a pointer int sscanf (const char *s,const char *format,...); The SSCANF function is scanf equivalent, but the input character of the former is derived from the string s

  

character input/output functions

int fgetc (file *stream); The function returns the next character of a stream stream, returns the type unsigned char if the end of the file is reached or an error occurs, returns EofChar *fgets (char *s,int n,file *stream) The Fgets function reads a maximum of n-1 characters into the array s when a newline character is encountered, reads the newline character into the array s, and the read process terminates the array to reach the end of the file at the end of ' \ s ' or an error occurs, returning eofint fputc (int c,file *stream); The FPUTC function outputs the character C to the stream and returns the written character, with an error returning Eofint fputs (const char *s,file *stream), the Fputs function outputting the string s to the stream, and an error returning Eofint getc ( FILE *stream); the GETC function is equivalent to fgetc, unlike when the GETC function is defined as a macro, it may compute the value of the stream multiple times int getchar (void); equivalent to GETC (stdin); Char *gets (char *s  The next input line is read into an array of int putc (int c, FILE *stream), int putchar (int c), equivalent to PUTC (c,stdout); int puts (const char *s); int ungetc (int C,file *stream);

  

direct input/output functions

size_t fread (void *ptr,size_t size,size_t nobj,file *stream); size_t fwrite (const void *ptr,size_t size,size_t Nobj,file * Stream);

  

File Locator function

int fseek (file *stream,long offset,int origin); Long Ftell (file *stream); void Rewind (file *stream); int fgetpos (FILE * stream,fpos_t *ptr); int Fsetpos (FILE *stream,const fpos_t *ptr);

  

Error Handling functions

When an error occurs or the end of the file is reached, many functions in the standard library set the status indicator, which indicates that the used by itself is explicitly set and tested in addition, an integer expression errno can contain an error number to further understand the last error message void Clearerr (FILE * stream); The Clearerr function clears the end of the file associated with the stream stream to conform to the error indicator int feof (file *stream), and if the end character of the stream is set, the Feof function returns a non-0 value int ferror ( FILE *stream), void perror (const char *s), fprintf (stderr, "%s,:%s\n", S "error message");

  

C Programming language Note (15) Reference manual 7

Related Article

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.