Programming input and output of the subject

Programming input and output of the subject

In general, in order to facilitate the input output, many people like cin, cout. Generally no problem, but the cout output format control is more complicated, but also for some topics output of more than 1e6 1e5 even then, you need to pay attention to the use cin may TLE. Synchronization is off course a solution, using the best or the use scanf and printf

Output line problem

1.gets

char *gets(char *s);

gets read from stdin s line to the designated buffer, the reading when the end of a line break or EOF. When the reading is successful, it s return address; failure returns null. Note that, '\ n' EOF character or alternatively gets to the end of the line will be '\ 0' , so, gets the read contents do not include '\ n' character. If you want to acquire the read length of the string, call strlen function can be obtained.

2.fgets

char *fgets(char *s, int size, FILE*stream);  

fgets(buff,10,stdin);//fgets头文件为cstdio

fgets content read from the stream up to the size of the size-1 s specified buffer, the reading when the end of a line break or EOF. When the reading is successful, it s return address; failure returns null. Note that, fgets added '\ 0' to the end of the read contents, so that the contents of the read fgets will include '\ n' characters at the end of the line . If you want to acquire the read length of the string, call strlen function can be obtained.

3.getline
for the C ++ language, if the C string, then it uses cin.getline () function, if a string, then the string type, on the use of global function getline (cin, n);

Note that these two functions do not read the last line breaks.

string s;
char str[256];
getline(cin, s);
cin.getline(str, sizeof(str));  

Guess you like

Origin www.cnblogs.com/gzr2018/p/12454985.html