C Standard Library (2) -- # include (stdio. h)

Source: Internet
Author: User

C Standard Library (2) -- # include (stdio. h)
C language standard library (2) -- # include By jxlijunhao is included in this header file, including single-character and string processing functions, standard input operations, stream operations, read from files, Write Functions, block I/O operations, etc. Part of the record below
Standard Input and Output:

Int getchar (void); // read a character from the standard input
Int putchar (int character); // output to standard output

Stream operation:
Int putc (int character, FILE * stream); // write characters to an input stream
Int getc (FILE * stream); // read a character from the output stream

Which is equivalent to the above functions?
int fgetc ( FILE * stream );
int fputc ( int character, FILE * stream );

FILE * fopen (const char * filename, const char * mode); // open the FILE
"R": Read data "w": If the opened file name already exists, the original content will be cleared and the new content will be written again. "r + ": read/update "w +": Write/update. If the file exists, the old content is cleared. "a +": Add new content to the file.

int fclose ( FILE * stream );

The following is a simple example. input data from a standard input and save it to a file.
# Include
 
  
Int main () {// read a group of data from the keyboard. If the input contains '#', it ends and is saved to the text FILE char c; FILE * pFile; pFile = fopen ("myFile.txt", "w +"); while (1) {c = getchar (); if (c = '#') break; fputc (c, pFile) ;}fclose (pFile); // read the data in the file and show pFile = fopen ("myFile.txt", "r"); if (pFile! = NULL) {while (c! = EOF) {c = fgetc (pFile); putchar (c) ;}} fclose (pFile );}
 

Block I/O operations: File Location
Int fseek (FILE * stream, long int offset, int origin); // FILE Location
Start with the third parameter and start offset calculation. There are three optional values: SEEK_SET: Start from the file SEEK_CUR: Start from the file SEEK_END: from the end of the file (note that the offset value must be set to a negative number, starting from-1)

In the following example, replace the 5th position in "this is a tste file" with lowercase letters.
Int main () {FILE * pFile; pFile = fopen ("test.txt", "wb"); fputs ("this is a test file. ", pFile); fseek (pFile, 5, SEEK_SET); // fputs (" I ", pFile); fclose (pFile );}




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.