|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 moc 于 2019-3-27 09:18 编辑
1、流概念和流库结构
程序的输入:数据从输入文件到程序;
程序的输出: 数据程序到输出文件。
标准I/O: 系统指定的标准的设备输入和输出,即即键盘的输入和显示屏的输出。
C++s输入输出流的三个方面:
① 键盘输入数据,输出数据到显示屏的标准I/O;
② 外存磁盘文件为对象进行的输入和输出,简称文件I/O;
③ 对内存空间指定位置的输入和输出,通常是指定一个字符数组作为存储空间,对字符串的输入输出,简称串I/O.
流库结构图:
ios:抽象基类,input output stream 的简称,流库的顶层基类。
iostream: 标准的IO流库
fstream: 文件I/O流库
2、标准I/O 输入和输出
从缓冲区读取数据时,如果缓冲区没有数据,程序将陷入阻塞状态。
标准输入流对象cin:
cin.get() //读取单个字符
cin.get(一个参数)
cin.get(三个参数)
cin.getline()
cin.ignore()
cin.peek()
cin.putback()
- int main()
- {
- char mybuf[128];
- int myInt;
- long myLong;
- cout << "1*************\n";
- // cin标准输入 遇见空格停止接受
- cin >> mybuf;
- cin >> myLong >> myInt;
- cout << "mybuf:" << mybuf << "mylong:" << myLong << "myInt:" << myInt << endl;
- cout << "2*************\n";
- // cin.get() 读取单个字符
- char ch, a, b, c;
- /* while ((ch = cin.get() != EOF)) // EOF ==》 ctrl+z
- {
- cout << ch << endl;
- }
- */ // 也可以这样
- cin.ignore(1); // 拿出缓冲区一个数据并丢弃
- cin.get(a).get(b).get(c); // 链式编程
- cout << "a:" << a << "b:" << b << "c:" << c << endl;
- cin.ignore(1);
- cout << "3*************\n";
- // geyline 可以接受空格(按行接受数据)
- char buf1[128], buf2[128];
- cin.getline(buf1, 128); // 可以接受带空格的字符串
- cout << buf1 << endl;
- cout << "4*************\n";
- // peak() 读出缓冲区的第一个字符,并不取走
- int myint = cin.peek();
- cout << "myint:" << myint << endl;
- cin.getline(buf2, 128);
- cout << buf2 << endl;
- cout << "5*************\n";
- // 吐回字符给缓冲区
- // 案例: 输入的整数和字符串分开
- cout << "please enter a number or a word: ";
- char cc = std::cin.get();
- if ((cc >= '0') && (cc <= '9'))
- {
- int n;
- cin.putback(cc);
- cin >> n;
- cout << "you enter a number:" << n << endl;
- }
- else
- {
- string str;
- cin.putback(cc);
- getline(cin, str); // <string> 从键盘输入到string
- }
- system("pause");
- return 0;
- }
复制代码
标准输出流对象cout:
cout.flush()
cout.put()
cou.write()
cout.fill()
cout.width()
cout.setf(标记)
- int main()
- {
- // 输出单个字符
- cout.put('h').put('i').put('!').put('\n');
- // 输出指定个字符
- char *p = "hello 你好!\n";
- cout.write(p, strlen(p));
- cout.write(p, strlen(p)-5);
- cout << endl;
- // 使用cout类的成员函数
- cout << "<start>";
- cout.width(30);
- cout.fill('*');
- cout.setf(ios::showbase);
- cout.setf(ios::internal);
- cout << hex << 123 << "<end>\n";
- // 使用操作符 // #include <iomanip>
- cout << "<start>"
- << setw(30)
- << setfill('*')
- << setiosflags(ios::showbase) //基数
- << setiosflags(ios::internal)
- << hex //开启后后面都以16进制显示
- << 123
- << "<end>\n"
- << endl;
- system("pause");
- return 0;
- }
复制代码
输出mainpulator(操作符、控制符):
flush endl oct dec hex setbase setw setfill setprecision ....
3、C++文件流
需要头文件:#include "fstream"
1. 打开文件
打开文件只是一种形象的说法,本质是文件读写之前的必要的准备工作。
① 为文件流对象和磁盘文件建立关联,以便文件流流向指定的磁盘文件;
② 指定文件的工作方式,如文件是输入还是输出,是ASCII文件还是二进制文件。
文件流成员函数 open
输出文件流: ofstream outfile; outfile.open("c:/...", ios::out)
输入文件流: ifstream 用于从文件读取信息。
文件流: fstream 同时具有 ofstream 和 ifstream 两种功能,这意味着它可以创建文件,向文件写入信息,从文件读取信息。
open() 成员函数或构造函数 第一参数指定要打开的文件的名称和位置,第二个参数定义文件被打开的模式。
ios::app 追加模式。所有写入都追加到文件末尾。
ios::ate 文件打开后定位到文件末尾。
ios::in 打开文件用于读取。
ios::out 打开文件用于写入。
ios::trunc 如果该文件已经存在,其内容将在打开文件之前被截断,即把文件长度设为 0。
文件位置指针
istream 和 ostream 都提供了用于重新定位文件位置指针的成员函数。这些成员函数包括关于 istream 的 seekg("seek get")和关于 ostream 的 seekp("seek put")。
seekg 和 seekp 的参数通常是一个长整型。第二个参数可以用于指定查找方向。
查找方向可以是 ios::beg(默认的,从流的开头开始定位),也可以是 ios::cur(从流的当前位置开始定位),也可以是 ios::end(从流的末尾开始定位)。
实例:
- int main()
- {
- // 输出文件流
- char* fname = "d:/1.txt";
- ofstream fout(fname, ios::out); //建立一个输出流对象 和文件关联
- fout << "hello...abc123" << endl;
- fout << "hello...你好" << endl;
- fout.close(); // 关闭流文件
- // 输入文件流
- ifstream fin(fname); // 建立一个输入流对象 和文件关联
- char ch;
- while (fin.get(ch)) // 从文件获取一个字符
- {
- cout << ch << " ";
- }
- fin.close();
- system("pause");
- return 0;
- }
复制代码
二进制文件读写:
- int main()
- {
- // 输出流
- char *fname = "d:/1.dat";
- ofstream fout(fname, ios::binary);
- if (!fout)
- {
- cout << "打开文件失败" << endl;
- return;
- }
- Teacher t1(12, "小明");
- Teacher t2(18, "小华");
- fout.write((char*)&t1, sizeof(Teacher));
- fout.write((char*)&t2, sizeof(Teacher));
- fout.close();
- // 输出流
- ifstream fin(fname);
- Teacher tmp;
- fin.read((char*)&tmp, sizeof(Teacher));
- cout << tmp.age << tmp.name << endl;
- fin.read((char*)&tmp, sizeof(Teacher));
- cout << tmp.age << tmp.name << endl;
- fin.close();
- system("pause");
- return 0;
- }
复制代码 |
|