目录
前言
对于C++的输入输出,总是会遇到一些小细节,然后会有一些疑惑。在学习了C++输入输出流后,未知的更多,但是有了整体的一个概念!
学习!分享!感谢!
I/O流的概念和流类库的结构
程序的输入指的是从输入文件将数据传送给程序,程序的输出指的是从程序将数据传送给输出文件。
- C++输入输出包含以下三个方面的内容:
- 对系统指定的标准设备的输入和输出。即从键盘输入数据,输出到显示器屏幕。这种输入输出称为标准的输入输出,简称标准I/O。
- 以外存磁盘文件为对象进行输入和输出,即从磁盘文件输入数据,数据输出到磁盘文件。以外存文件为对象的输入输出称为文件的输入输出,简称文件I/O。
- 对内存中指定的空间进行输入和输出。通常指定一个字符数组作为存储空间(实际上可以利用该空间存储任何信息)。这种输入和输出称为字符串输入输出,简称串I/O。主要用于像共享内存这样的。
标准输入输出流
标准输入流对象cin常见函数
cin >> a; // a如果是字符串,cin遇到空格返回,并且把空格保留在输入缓冲区
cin.get(); // 获取一个字符
cin.get(一个参数);
cin.getline();
cin.ignore();
cin.peek();
cin.putback();
cin获取数据
int main()
{
int myInt;
long myLong;
char myBuf[1024];
cin >> myInt;
cin >> myLong;
cin >> myBuf; // 遇见空格停止接收数据
cout << "myInt=" << myInt << " "
<< "myLong=" << myLong << " "
<< "myBuf=" << myBuf << endl;
return 0;
}
cin.get()
// cin.get()如果在输入缓冲区中没有检测到EOF就会使得程序阻塞
// 这时候要往输入缓冲区中输入Crl+Z,然后回车,这样就是往缓冲区中发送EOF
int main()
{
char ch;
while((ch = cin.get()) != EOF)
{
cout << ch << endl;
}
return 0;
}
cin.get(一个参数)
int main()
{
char a, b, c;
cin.get(a); // 引用
cin.get(b);
cin.get(c);
cout << "a=" << a << " "
<< "b=" << b << " "
<< "c=" << c << endl;
cin.get(a).get(b).get(c); // cin.get()返回值为一个in对象
cout << "a=" << a << " "
<< "b=" << b << " "
<< "c=" << c << endl;
return 0;
}
cin.getline(x, xx, xxx)
int main()
{
char buf1[256];
char buf2[256];
cin >> buf1; // cin在得到空格之后就会返回,但是空格还是保留在缓冲区
cin.getline(buf2, 256, '\n');
cout << "buf1=" << buf1 << " "
<< "buf2=" << buf2 << endl;
// aa bb cc dd // 程序输入
// buf1=aa buf2= bb cc dd // 程序输出
return 0;
}
cin.ignore()和cin.peek()
int main()
{
char buf1[256];
char buf2[256];
cin >> buf1; // cin在得到空格之后就会返回,但是空格还是保留在缓冲区
cin.ignore(1); // 可以忽略缓冲区中的元素 可以用来忽略空格
char myChar = cin.peek(); // 查看当前缓冲区中的数据 程序是阻塞式的 如果没有检测到数据就会阻塞而不会退出
cout << "myChar=" << myChar << endl;
cin.getline(buf2, 256, '\n');
cout << "buf1=" << buf1 << " "
<< "buf2=" << buf2 << endl;
// aa bb cc dd // 程序输入
// buf1=aa buf2=bb cc dd // 程序输出
return 0;
}
cin.putback()
int main()
{
cout << "Please enter a number or a word" << endl;
char c = cin.get();
if((c >= '0')&&(c <= '9'))
{
int n;
cin.putback(c);
cin >> n; // in会自动判断哪些是数字 同时如果数字后面后字母 不予理会
cout << "You entered a number:" << n << endl;
}
else
{
string str;
cin.putback(c);
getline(cin, str);
cout << "You entered a word:" << str << endl;
}
return 0;
}
标准输出流对象cout常见函数
cout.flush();
cout.put();
cout.write();
cout.width();
cout.fill();
cout.set(标记);
cout.put()和cout.write()
#include <iostream>
#include <string.h>
#include <iomanip>
using namespace std;
int main()
{
cout << "hello" << endl;
cout.put('h').put('e').put('l').put('l').put('o') << endl;
char *p = "hello cout";
cout.write(p, strlen(p)) << endl;
cout.write(p, strlen(p)-3) << endl;
cout.write(p, strlen(p)+3) << endl;
return 0;
}
cout.fill()和cout.setf()和cout.width()
#include <iostream>
#include <string.h>
#include <iomanip>
using namespace std;
int main()
{
cout << "<start>";
cout.width(30);
cout.fill('-');
cout.setf(ios::showbase); // #include <iomanip> // 显示进制
cout.setf(ios::internal);
cout << hex << 123 << endl;
cout << "<start>"
<< setw(30)
<< setfill('*')
<< setiosflags(ios::showbase) // ios类中的静态变量
<< setiosflags(ios::internal)
<< hex
<< 123
<< endl;
return 0;
}
总结
在《传智播客扫地僧c++基础和进阶课堂讲义.docx》这份文档中有更详细的介绍,开始准备把所有的知识都看一遍,但是后来发现没有必要其实。输入输出部分用来查阅最好不过了!感谢前人的辛苦积累,以后努力写些自己的感悟,希望帮助更多的人吧!
附上《传智播客扫地僧c++基础和进阶课堂讲义.docx》下载地址。
本文详细介绍了C++中的输入输出流概念及应用,包括标准输入输出流、文件I/O及字符串I/O等内容,并提供了cin与cout对象的常用函数示例。
1540





