io20_2_1.02.c

来自「C++ Primer(第三版)的随书源代码」· C语言 代码 · 共 42 行

C
42
字号
// #include <iostream>
// #include <iomanip>

#include <iostream.h>
#include <iomanip.h>

/*
 * the program generates the following unexpected results:
 *  $ a.out
 *  The winter of our discontent
 *
 *  The
 *  win
 *  ter
 *  of
 *  our
 *  dis
 *  con
 *  ten
 *  t
 *
 * The problem is that setw() is passed the size
 * of the character pointer rather than the size
 * of the character array to which it points.
 *
 * correct: while ( cin >> setw( bufSize ) >> pbuf )
 */

int main()
{
	const int bufSize = 24;
	char buf[ bufSize ];
	char *pbuf = buf;

	// each string greater than sizeof(char*)
	// is broken into two or more strings

	while ( cin >> setw( sizeof( pbuf )) >> pbuf )
 		cout << pbuf << endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?