c++字符串输入函数总结
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Input was: This is a test ============================= 8. 从文件流中读取一个字符串,直到遇到换行符,而且换行符也会被读入,并且不会自动转换 为'\0';或者读入 n-1 个字符后自动添加一个'\0',有标准和宽字符两种版本。 Get a string from a stream. char *fgets(
Example // basic_istream_get.cpp // compile with: /EHsc #include <iostream> using namespace std; int main( ) {
char c[10]; c[0] = cin.get( ); //读取一个字符,存入 c[0] cin.get( c[1] ); //读取一个字符,存入 c[1] cin.get( &c[2],3 );//读取 3 个字符,从 c[2]开始存放,或遇到'\n' cin.get( &c[4], 4, '7' );//读取 4 个字符,从 c[4]开始存放,或遇到'7' cout << c << endl; } Input 11 Output 11 ============================= 3. cin.getline() 从标准输入读取一行,有下面两种重载,没有指定终结符时,实际上默认为'\n'。例子中&c[0] 等于直接写 c,这样写只是说明可以指定精确存放位置。 basic_istream& getline( char_type *_Str, streamsize _Count ); basic_istream& getline( char_type *_Str, streamsize _Count, char_type _Delim ); Example // basic_istream_getline.cpp // compile with: /EHsc #include <iostream> using namespace std; int main( ) { char c[10]; cin.getline( &c[0], 5, '2' ); cout << c << endl; } Input 12
&& (ch != '\n'); i++ ) buffer[i] = (char)ch; /* Terminate string with null character: */ buffer[i] = '\0'; printf( "Input was: %s\n", buffer ); } Input This is a test Output
// Much preferable: fgets( line, 21, stdin ); // but you'd have to remove the trailing '\n' printf( "The line entered was: %s\n", line ); } Input Hello there! Output The line entered was: Hello there! Note that input longer than 20 characters will overrun the line buffer and almost certainly cause the program to crash. ================== 6. 从终端读取一个字符,有标准和宽字符两者,一组带回显,一组不带。注意返回值是 int(或 wint_t) Get a character from the console without echo (_getch, _getchw) or with echo (_getche, _getwche). int _getch( void ); wint_t _getwch( void ); int _getche( void ); wint_t _getwche( void ); Example // crt_getch.c // compile with: /c /* This program reads characters from * the keyboard until it receives a 'Y' or 'y'. */ #include <conio.h> #include <ctype.h> int main( void ) { int ch; _cputs( "Type 'Y' when finished typing keys: " ); do { ch = _getch(); ch = toupper( ch );
string s1; cout << "Enter a sentence (use <space> as the delimiter): "; getline(cin,s1, ' '); cout << "You entered: " << s1 << endl;; } Input test this Sample Output Enter a sentence (use <space> as the delimiter): test this You entered: test Requirements Header: <string> ==================== 5. gets()/_getws() 从标准输入流读取一行,结尾的换行符'\n'会被替换成'\0'。注意它不检查缓存大小,因此可 能造成缓冲区溢出漏洞,尽量少用。 Get a line from the stdin stream. char *gets( char *buffer ); wchar_t *_getws( wchar_t *buffer
); Example // crt_gets.c #include <stdio.h> int main( void ) {
char line[21]; // room for 20 chars + '\0' gets( line ); // Danger: No way to limit input to 20 ars.
读取4个字符从c4开始存放或遇到?7?coutinput11output11cingetline从标准输入读取一行有下面两种重载没有指定终结符时实际上默认为?n?
看了网上有人写的,不是很全,而且还有几处错误,所以自己重新找了一下 MSDN 中的相 关内容。
1. cin/wcin 标准 C++输入流,有 ANSI 版本和宽字符版本,用法基本相同,不用多说,下面的例子是 cerr, 中间也用到了这两个输入函数。 Example // iostream_cerr.cpp // compile with: /EHsc // By default, cerr and clog are the same as cout #include <iostream> #include <fstream> using namespace std; void TestWide( ) {
char *string, int n, FILE *stream ); wchar_t *fgetws( wchar_t *string, int n, FILE *stream ); Example // crt_fgets.c /* This program uses fgets to display * a line from a file on the screen. */ #include <stdio.h> int main( void ) { FILE *stream; char line[100]; if( (stream = fopen( "crt_fgets.txt", "r" )) != NULL ) {
Output 1 ============================== 4. getline() 从键盘读取一行,可以不指定终结符,默认为'\n',当然也可以像例子中指定空格。这个是 string 流,使用时需要包含头文件<string>,注意与前面的 cin.getline()区别。 Example // string_getline_sample.cpp // compile with: /EHsc // Illustrates how to use the getline function to read a // line of text from the keyboard. // // Functions: // // getline Returns a string from the input stream. ////////////////////////////////////////////////////////////////////// #pragma warning(disable:4786) #include <string> #include <iostream> using namespace std ; int main() {
int i = 0; wcout << L"Enter a number: "; wcin >> i; wcerr << L"test for wcerr" << endl; wclog << L"test for wclog" << endl; } int main( ) { int i = 0; cout << "Enter a number: "; cin >> i; cerr << "test for cerr" << endl; clog << "test for clog" << endl; TestWide( ); } Input 3 1 Sample Output Enter a number: 3 test for cerr test for clog Enter a number: 1 test for wcerr test for wclog =========================== 2. cin.get() 用来读入字符或字符串,可以设置读取的个数和终结字符,而且如下示例可以指定存放的精 确位置。注意最后需要留一个位置用来存放'\0'。
} while( ch != 'Y' ); _putch( ch ); _putch( '\r' ); /* Carriage return */ _putch( '\n' ); /* Line feed */ } Input abcdey Output Type 'Y' when finished typing keys: Y ============================ 7. 从流(getc, getwc)或者标准输入(getchar, getwchar)读取一个字符,有标准和宽字符两种版本, 返回值也是 int(或 wint_t)。 Read a character from a stream (getc, getwc), or get a character from stdin (getchar, getwchar). int getc( FILE *stream ); wint_t getwc( FILE *stream ); int getchar( void ); wint_t getwchar( void ); Example // crt_getc.c /* This program uses getchar to read a single line * of input from stdin, places this input in buffer, then * terminates the string before printing it to the screen. */ #include <stdio.h> int main( void ) { char buffer[81]; int i, ch; /* Read in single line from "stdin": */ for( i = 0; (i < 80) && ((ch = getchar()) != EOF)