⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 putbackdog.cc

📁 gcc-2.95.3 Linux下最常用的C编译器
💻 CC
字号:
/* Copyright (C) 1993 Free Software FoundationThis file is part of the GNU IO Library.  This library is freesoftware; you can redistribute it and/or modify it under theterms of the GNU General Public License as published by theFree Software Foundation; either version 2, or (at your option)any later version.This library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this library; see the file COPYING.  If not, write to the FreeSoftware Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.As a special exception, if you link this library with filescompiled with a GNU compiler to produce an executable, this does not causethe resulting executable to be covered by the GNU General Public License.This exception does not however invalidate any other reasons whythe executable file might be covered by the GNU General Public License. */// Test streambuf::sputbackc#include <iostream.h>#include <stdlib.h>#include <string.h>// Read either "dog", "hound", or "hounddog".// If "dog" is found, return 1.// If "hound" is found, return 2.// If "hounddog" is found, return 3.// If non of these are found, return -1.void unget_string(streambuf *sb, char *str, int count){    for (str += count; -- count >= 0; )	sb->sputbackc(*--str);}int my_scan(streambuf* sb){    char buffer[20];    // Try reading "hounddog":    int count;    count = sb->sgetn(buffer, 8);    if (count == 8 && strncmp(buffer, "hounddog", 8) == 0)      return 3;    // No, no "hounddog":  Backup to 'fence' ...    unget_string(sb, buffer, count);    // ... and try reading "dog":    count = sb->sgetn(buffer, 3);    if (count == 3 && strncmp(buffer, "dog", 3) == 0)      return 1;    // No, no "dog" either:  Backup to 'fence' ...    unget_string(sb, buffer, count);    // ... and try reading "hound":    count = sb->sgetn(buffer, 5);    if (count == 5 && strncmp(buffer, "hound", 5) == 0)      return 2;    // No, no "hound" either:  Backup to 'fence' and signal failure.    unget_string(sb, buffer, count);    return -1;}int main(int argc, char **argv){    streambuf *sb = cin.rdbuf();    if (argc > 1 && strncmp(argv[1], "-b", 2) == 0) {	streambuf *ret;	int buffer_size = atoi(&argv[1][2]);	if (buffer_size == 0)	    ret = sb->setbuf(NULL, 0);	else	    ret = sb->setbuf(new char[buffer_size], buffer_size);	if (ret != sb)	    cerr << "Warning: cin.rdbuf()->setbuf failed!\n";    }    for (;;) {	int code = my_scan(sb);	int ch = sb->sbumpc();	if (code == -1 && ch == EOF)	    break;	int n = 0;	while (ch != EOF && ch != '\n') {	    n++;	    ch = sb->sbumpc();	};	if (ch == EOF) {	    cout << "[Unexpected EOF]\n";	    break;	}	cout << "Code: " << code << " followed by " << n << " chars\n";    }}

⌨️ 快捷键说明

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