标签:style blog class code tar ext
/*File : cerr.cpp *Auth : sjin *Date : 20140506 *Mail : 413977243@qq.com */ #include <iostream> using namespace std; /*格式化输出 *输出缓冲遇到换行,有输入、满、程序结果、flush会刷新 */ int main() { char ch; cerr << "World "; /*无缓冲,不可重定向*/ cin >> ch; clog << "Clog "; /*理论上“缓冲,不可重定向”实际上无缓冲*/ cout << "Hello "; /*缓冲,可重定向*/ //输入任意字符退出 cin >> ch; return 0; }
/*File : get.cpp *Auth : sjin *Date : 20140506 *Mail : 413977243@qq.com */ #include <iostream> using namespace std; int main() { char ch; while((ch = cin.get()) != EOF ){ if (ch == ‘$‘){ break; } cout <<"ch = " << ch << endl; } cout << "Done" << endl; cin.ignore(10,‘\n‘); char s[25]; cin.get(s,25); cout << s << endl; return 0; }
/*File : cerr.cpp *Auth : sjin *Date : 20140506 *Mail : 413977243@qq.com */ #include <iostream> #include <string> using namespace std; /*i.get() 从键盘输入一个字符 *o.put() 往屏幕输出一个字符 *i.getline() 从键盘输入一串字符串 *i.putback() 插入字符。 *i.peek() //查看输入缓冲区的第一个字符 */ void get_put() { cout << "####请输入四个字符或者数字###" << endl; int n = cin.get(); char c,d,e; cin.get(c).get(d).get(e);// istream& get(char& ch); cout << "n = " <<n << endl; cout << "c = " <<c << endl; cout << "d = " <<d << endl; cout << "e = " <<e << endl; cout << "另一种输出方法" <<endl; cout.put(n).put(c).put(d).put(e); //忽略前200个字符除非遇到字符‘\n‘,而停止忽略字符 cin.ignore(200,‘\n‘); char ch; cin >> ch; cout << "ch = " <<ch << endl; cin.ignore(200,‘\n‘); } void mygetline() { char buf[10]={‘\0‘}; cout << "####start getline ###" <<endl; cout << "cin.getline 输入字符串,长度不要超过9个,‘\0‘ 10个" <<endl; cin.clear(); if(!cin.getline(buf,sizeof(buf))){ cout << "行输入过长,错误" <<endl; cin.clear(); /*清除输入过长的部分,应该不会超过1000个吧!*/ cin.ignore(1000,‘\n‘); } string s; cout << "使用getline 全局函数输入字符串:" <<endl; getline(cin,s);//getline(cin,s,‘s‘) cout << "buf = " << buf<<endl; cout << "s = " << s << endl; cout << "printf 输出s :" <<endl; printf("%s\n",s.c_str()); cout << "####end getline ###" <<endl; } void mypeek() { char buf[10]; char c; cout << "请输入一个字符," << endl; cin >>ws;//屏蔽掉空格 c = cin.get(); cin.putback(c); if(cin.peek()> ‘0‘ && cin.peek() < ‘9‘){ double d; cout <<"请输入一个浮点数" << endl; cin >> d; cout << "d = " << d << endl; }else{ string s; cout <<"请输入一串字符串" << endl; cin >> s ; cout <<"s = " << s << endl; } } int main2() { //get_put(); //mygetline(); //mypeek(); return 0; }
参考资料:
走进C++程序世界------IO标准库介绍,布布扣,bubuko.com
标签:style blog class code tar ext
原文地址:http://blog.csdn.net/sjin_1314/article/details/25533005