I summarize the commonly used input and output function __ function

Source: Internet
Author: User
Tags stdin

(i) standard input and output:

printf ("%d", n);

scanf ("%d", &n);


(ii) file input and output:

fprintf (FIN, "%d", n);/FILE *fin = open ("In.txt", "RB");

FSCANF (Fout, "%d", &n);/FILE *fout = open ("OUT.txt", "WB");


(iii) input and output redirection:

1.freopen ("In.txt", "RB", stdin);

Freopen ("Outin.txt", "WB", stdout);

In C, the use of scanf () and printf () is read and stored from the file

In C + +, the use of CIN and cout is read and stored from the file

2. The standard file input and output in C + + is:

#include <fstream>

using namespace Std;

Ifstream fin ("in.txt");

Ofstream fout ("OUT.txt");

fin>>a>>b;

fout<<a<<b;


(iv) Input output in string

sprintf (CH, "%d", a);/char Ch[max] to ensure that the CH array is large enough

SSCANF (CH, "%d", &a);


(v) Other

(1) fgetc (Fin);//fin is a file pointer

Function: Reads a character from a file and returns an integer

(2) GetChar ();

Function: read a character from standard input, similar to fgetc, but with different input methods.

(3) fgets (CH, MAXN, Fin),//char Ch[max], MAXN read no more than MAXN-1 characters, and then end with a Terminator ' "; FILE *fin;

Function: Read the complete line from the file and read the "\ n" End of the return character.

can also be used as a keyboard input: fgets (Ch,n,stdin); The advantage of using this is to read data of a specified size, avoiding cache overflow problems caused by the gets function receiving a string from stdin without checking the volume of the cache it replicates

(4) gets (CH);//char Ch[max];

Function: Reads a line of strings from the standard input output, can read spaces and TEB, and reads to the carriage return ' \ n ' end. But it does not limit the number of inputs, so it is easy to buffer overflow, not recommended.


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.