C++-标准输入输出
1,cout
1) 用来向标准输出打印。
2) 如果参数是char*类型,则直接输出字符串。如果想要输出地址,则需要强制转换:
<<static_cast<void*>(const_cast<char*>(pstr));
2,cin
1) 将标准输入读入变量,如果输入与变量声明类型不一致,则cin为false,可以用if判断,变量值不确定。
double price; //输入asdf
cout << price; //输出为0
2) cin结束后在缓存中会遗留有'\n',因此会影响后续的get/getline。未不影响后续使用可以调用cin.ignore()
3, cout.get()和cout.getline()
1) 都是用来从标准输入读入内容,可以控制读入,cin遇到回车符就会结束,并且用空格分隔变量。get和getline则不同,它们可以控制读入的长度和分隔符。
2) 区别很小,都是用来读入流。同cin类似get不会从缓存中移除'\n',getline则不同,可以清除。所以要么一直用cout/cin组合,要么一直用getline,getline,getline,不要用cin或者get后用get或getline。
4, read()和write()也是cout的函数,和标准的cout/cin不同的是它们可以控制输出和输入字节数。
5, 有个global函数getline挺好用的。
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std; int main()
{
//cout
cout << "cout test started..." << endl;
const char* str = "adfadf";
cout << "str is: " << str << endl; //cin
//read input seperated by space
//if input is not the cin type, cin will be false
cout << "cin test started..." << endl;
string sname;
double price;
cout << "Please enter name and price: " << endl;
cin >> sname;
if(!cin)
cout << "the name is incorrect. " << endl;
cin >> price;
if(!cin)
cout << "the price is incorrect. " << endl;
cout << "The name is: " << sname
<< " and the price is: " << price << endl; //cout ignore one \n
cin.ignore(); //get, address will fail to get because get left a \n after first call
// // cout << "Enter your name:";
// char name[SIZE];
// // cin.get(name,SIZE);
// // cout << "name:" << name;
// // cout.put(name[0]);
// // cout << "\nEnter your address:";
// char address[SIZE];
// // cin.get(address,SIZE);
// // cout << "address:" << address << endl; //getline, address will succeed to get
const int SIZE = ;
cout << "Enter your name:";
char name[SIZE];
cin.getline(name,SIZE);
cout << "name:" << name;
cout << "\nEnter your address:";
char address[SIZE];
cin.getline(address,SIZE);
cout << "address:" << address << endl; //global function getline
cout << "global getline test start" << endl;
string ss;
getline(cin, ss);
cout << "ss is: " << ss << endl; //read, write
char inchar[];
cin.read(inchar, );
cout.write(inchar, ); }
C++-标准输入输出的更多相关文章
- C/C++ 标准输入输出重定向
转载自:http://www.cnblogs.com/hjslovewcl/archive/2011/01/10/2314356.html 这个对经常在OJ上做题的童鞋们很有用.OJ基本都是用标准输入 ...
- C语言的标准输入输出
1. 标准输入输出 标准输入.输出主要由缓冲区和操作方法两部分组.缓冲区实际上可以看做内存中的字符串数组,而操作方法主要是指printf.scanf.puts.gets,getcha.putcahr等 ...
- linux标准输入输出及错误输出
Linux Shell 环境中支持输入输出重定向,用符号"<"和">"来表示. 0.1和2分别表示标准输入.标准输出和标准错误信息输出,可以用来指定 ...
- 【转载】标准输入输出重定向(Visual C++)
原文:标准输入输出重定向(Visual C++) 引言 本人偶得在 Visual C++ 中进行输入输出重定向的办法,比通常的做法“freopen”更加的灵活和方便,特在此共享.目前,代码正在不断地摸 ...
- linux标准输入输出2>&1
linux中有三种标准输入输出,分别是STDIN,STDOUT,STDERR,对应的数字是0,1,2. STDIN是标准输入,默认从键盘读取信息:STDOUT是标准输出,默认将输出结果输出至终 ...
- C++之标准输入输出
由于在公司,无法上传图片,往后补上. 关于C++的标准输入输出,其实就相当于C语言的scanf和printf,只不过C++用cin和cout这样称为流的机制. #include <iostrea ...
- 标准输入输出 sys.stdin与sys.stdin
1.python中的标准输入输出 如果需要更好的控制输出,而print不能满足需求,input也不能 sys.stdout,sys.stdin,sys.stderr就是你需要的. 2.输入:sys.s ...
- Python学习笔记015——文件file的常规操作之三(标准输入输出文件)
1 标准输入输出文件 在Python中,模块sys中含有标准的输入输出文件 sys.stdin 标准输入方法(一般是键盘) sys.stdout 标准输出方法(到显示器的缓冲输出) sys ...
- Python基础笔记系列十一:标准输入输出、文件读写和指针等操作
本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! 标准输入输出一.输入 在sublime中这个时候需要安装SublimeRE ...
- linux标准输入输出错误输出
Linux Shell 环境中支持输入输出重定向,用符号"<"和">"来表示.0.1和2分别表示标准输入.标准输出和标准错误信息输出,可以用来指定需 ...
随机推荐
- Delphi 使用之函数
函数由一句或多句代码组成,可以实现某个特定的功能.使用函数可以使代码更加易读.易懂,加快编程速度及减少重复代码.过程与函数类似,过程与函数最重要的区别在于,过程没有返回值,而函数能有返回值. ...
- [js] 函数节流
原文链接:http://www.alloyteam.com/2012/11/javascript-throttle/
- word2010表格中的内容怎么设置行距
选中表格,然后根据箭头指示点击 弹出如下对话框,选择行距
- this和call
function foo(x){ console.log(x);} foo.call(this,'abc');console.log(this); ---- abc
- R统计建模与R软件
教材目录 第一章 概率统计的基本知识 第二章 R软件的使用 第三章 数据描述性分析 第四章 参数估计 第五章 假设检验 第六章 回归分析 第七章 方差分析 第八章 应用多元分析(I) 第九章 应用多元 ...
- javaWeb1 tomcat
tomcat使用常见问题: 1.闪退: 原因:tomcat 软件是由java语言开发的,当它启动时,会默认到系统 的环境变量中查找 JAVA_HOME 的变量.找它的目的时tomcat 启动 时需要j ...
- SAP MM模块之批次管理
1.Batch的定义:Batch is a quantity any drug produced during a given cycle of manufacture. The essence of ...
- svn 大杂烩
svn : trunk 日常开发 branch 多版开发,或者修复bug,测试 tag 开发到一阶段打一个tag,给外部使用 属性externals:可以引用外部的公共工程.这个工程最好是稳定的,不 ...
- ubuntu安装SCrapy
依次安装 sudo apt-get install build-essential; sudo apt-get install python-dev; sudo apt-get install lib ...
- HDU----(3294)Girls' research(manacher)
Girls' research Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)T ...