一 I/O流的概念和流类库的结构
1 I/O流的概念和流类库的结构
1)标准设备的输入和输出。即从键盘输入数据,输出到显示器屏幕。标准输入输出
2)标准设备的输入和输出。即从键盘输入数据,输出到显示器屏幕。标准输入输出
3)内存中指定的空间进行输入和输出,称为字符串输入输出,简称串I/O
2 标准输入
#include <iostream>
using namespace std;
#include "string"
/*
标准输入流对象cin
cin.get() //一次只能读取一个字符
cin.get(一个参数) //读一个字符
cin.get(三个参数) //可以读字符串
cin.getline()
cin.ignore()
cin.peek()
cin.putback()
*/
void main71()
{
char mybuf[1024];
int myInt;
long myLong;
cin >> myInt;
cin >> myLong;
cin >> mybuf; // 遇见空格停止接受 数据
cout << "myInt:" << myInt << "myLong" << myLong << "mybuf:" << mybuf << endl;
system("pause");
}
void main72()
{
char ch;
while ( (ch=cin.get() )!= EOF )
{
cout << ch << endl;
}
system("pause");
}
void main73()
{
char a, b, c;
cout << "cin.get(a) 如果缓冲区没有数据,则程序阻塞 \n";
cin.get(a);
cin.get(b);
cin.get(c);
cout << a << b << c << "因为缓冲区有数据,程序不会阻塞\n";
cin.get(a).get(b).get(c);
cout << a << b << c;
system("pause");
}
//getline函数可以接受 空格
void main74()
{
char buf1[256];
char buf2[256];
cout << "请输入一个字符串 含有多个空格 aa bb cc dd\n";
cin >> buf1;
cin.getline(buf2, 256);
cout << "buf1:" << buf1 << "buf2:" << buf2 << endl;
system("pause");
}
void main75()
{
char buf1[256];
char buf2[256];
cout << "请输入一个字符串 含有多个空格aa bbccdd\n";
cin >> buf1;
cin.ignore(20);
int myint = cin.peek();
cout << "myint:" << myint << endl;
cin.getline(buf2, 256);
cout << "buf1:" << buf1 << "\nbuf2:" << buf2 << endl;
system("pause");
}
//案例:输入的整数和字符串分开处理
int main78()
{
cout << "Please, enter a number or a word: ";
char c = std::cin.get();
if ( (c >= '0') && (c <= '9') ) //输入的整数和字符串 分开处理
{
int n; //整数不可能 中间有空格 使用cin >>n
cin.putback (c);
cin >> n;
cout << "You entered a number: " << n << '\n';
}
else
{
string str;
cin.putback (c);
//cin.getline(str);
getline (cin, str); // //字符串 中间可能有空格 使用 cin.getline();
cout << "You entered a word: " << str << '\n';
}
system("pause");
return 0;
}
3标准输出
#include <iostream>
using namespace std;
#include <iomanip>
/*
标准输出流对象cout
cout.flush()
cout.put()
cout.write()
cout.width()
cout.fill()
cout.setf(标记)
*/
/*
manipulator(操作符、控制符)
flush
endl
oct
dec
hex
setbase
setw
setfill
setprecision
…
*/
void main81()
{
cout << "hello" << endl;
cout.put('h').put('e').put('l');
char *p = "hello itcast";
cout.write(p, strlen(p)) << endl;
cout.write(p, strlen(p) - 4) << endl;
cout.write(p, strlen(p) + 4) << endl;
system("pause");
return ;
}
void main82()
{
//使用类成员函数
cout << "<start>";
cout.width(30);
cout.fill('*');
cout.setf(ios::showbase); //#include <iomanip>
//cout.setf(ios::internal); //设置
cout << hex << 123 << "<End>\n";
cout << endl << endl;
//
//使用控制符
cout << "<Start>"
<< setw(30)
<< setfill('*')
<< setiosflags(ios::showbase) //基数
<< setiosflags(ios::internal)
<< hex
<< 123
<< "<End>\n"
<< endl;
system("pause");
}
int main()
{
int a;
cout<<"input a:";
cin>>a;
cout<<"dec:"<<dec<<a<<endl; //以十进制形式输出整数
cout<<"hex:"<<hex<<a<<endl; //以十六进制形式输出整数a
cout<<"oct:"<<setbase(8)<<a<<endl; //以八进制形式输出整数a
char *pt="China"; //pt指向字符串"China"
cout<<setw(10)<<pt<<endl; //指定域宽为,输出字符串
cout<<setfill('*')<<setw(10)<<pt<<endl; //指定域宽,输出字符串,空白处以'*'填充
double pi=22.0/7.0; //计算pi值
//按指数形式输出,8位小数
cout<<setiosflags(ios::scientific)<<setprecision(8);
cout<<"pi="<<pi<<endl; //输出pi值
cout<<"pi="<<setprecision(4)<<pi<<endl; //改为位小数
cout<<"pi="<<setiosflags(ios::fixed)<<pi<<endl; //改为小数形式输出
system("pause");
return 0;
}
int main22( )
{
double a=123.456,b=3.14159,c=-3214.67;
cout<<setiosflags(ios::fixed)<<setiosflags(ios::right)<<setprecision(2);
cout<<setw(10)<<a<<endl;
cout<<setw(10)<<b<<endl;
cout<<setw(10)<<c<<endl;
system("pause");
return 0;
}