鱼C论坛

 找回密码
 立即注册
查看: 2068|回复: 0

[学习笔记] 042-C++之输入和输出流

[复制链接]
发表于 2018-9-14 23:15:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 moc 于 2019-3-27 09:18 编辑

1、流概念和流库结构
程序的输入:数据从输入文件到程序;
程序的输出: 数据程序到输出文件。
标准I/O:  系统指定的标准的设备输入和输出,即即键盘的输入和显示屏的输出。
C++s输入输出流的三个方面:
        ① 键盘输入数据,输出数据到显示屏的标准I/O;
        ② 外存磁盘文件为对象进行的输入和输出,简称文件I/O;
        ③ 对内存空间指定位置的输入和输出,通常是指定一个字符数组作为存储空间,对字符串的输入输出,简称串I/O.
流库结构图:
976.jpg
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()
  1. int main()
  2. {
  3.         char mybuf[128];
  4.         int myInt;
  5.         long myLong;

  6.         cout << "1*************\n";
  7.         // cin标准输入  遇见空格停止接受
  8.         cin >> mybuf;   
  9.         cin >> myLong >> myInt;
  10.         cout << "mybuf:" << mybuf << "mylong:" << myLong << "myInt:" << myInt << endl;

  11.         cout << "2*************\n";
  12.         // cin.get()  读取单个字符
  13.         char ch, a, b, c;
  14. /*        while ((ch = cin.get() != EOF))  // EOF ==》 ctrl+z
  15.         {
  16.                 cout << ch << endl;
  17.         }
  18. */        // 也可以这样
  19.         cin.ignore(1);  // 拿出缓冲区一个数据并丢弃
  20.         cin.get(a).get(b).get(c);  // 链式编程
  21.         cout << "a:" << a << "b:" << b << "c:" << c << endl;
  22.         cin.ignore(1);

  23.         cout << "3*************\n";
  24.         // geyline  可以接受空格(按行接受数据)
  25.         char buf1[128], buf2[128];
  26.         cin.getline(buf1, 128);  // 可以接受带空格的字符串
  27.         cout << buf1 << endl;

  28.         cout << "4*************\n";
  29.         // peak()  读出缓冲区的第一个字符,并不取走
  30.         int myint = cin.peek();   
  31.         cout << "myint:" << myint << endl;
  32.         cin.getline(buf2, 128);
  33.         cout << buf2 << endl;

  34.         cout << "5*************\n";
  35.         // 吐回字符给缓冲区
  36.         // 案例: 输入的整数和字符串分开
  37.         cout << "please enter a number or a word: ";
  38.         char cc = std::cin.get();
  39.         if ((cc >= '0') && (cc <= '9'))
  40.         {
  41.                 int n;
  42.                 cin.putback(cc);
  43.                 cin >> n;
  44.                 cout << "you enter a number:" << n << endl;
  45.         }
  46.         else
  47.         {
  48.                 string str;
  49.                 cin.putback(cc);
  50.                 getline(cin, str);  // <string>  从键盘输入到string

  51.         }
  52.         system("pause");
  53.         return 0;
  54. }
复制代码

标准输出流对象cout:
        cout.flush()
        cout.put()
        cou.write()
        cout.fill()
        cout.width()
        cout.setf(标记)
  1. int main()
  2. {
  3.         // 输出单个字符
  4.         cout.put('h').put('i').put('!').put('\n');
  5.         // 输出指定个字符
  6.         char *p = "hello 你好!\n";
  7.         cout.write(p, strlen(p));
  8.         cout.write(p, strlen(p)-5);

  9.         cout << endl;
  10.         // 使用cout类的成员函数
  11.         cout << "<start>";
  12.         cout.width(30);
  13.         cout.fill('*');
  14.         cout.setf(ios::showbase);
  15.         cout.setf(ios::internal);
  16.         cout << hex << 123 << "<end>\n";

  17.         // 使用操作符    // #include <iomanip>
  18.         cout << "<start>"
  19.                 << setw(30)
  20.                 << setfill('*')
  21.                 << setiosflags(ios::showbase)  //基数
  22.                 << setiosflags(ios::internal)
  23.                 << hex   //开启后后面都以16进制显示
  24.                 << 123
  25.                 << "<end>\n"
  26.                 << endl;

  27.         system("pause");
  28.         return 0;
  29. }
复制代码

输出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(从流的末尾开始定位)。
实例:
  1. int main()
  2. {
  3.         // 输出文件流
  4.         char* fname = "d:/1.txt";
  5.         ofstream fout(fname, ios::out);  //建立一个输出流对象  和文件关联
  6.         fout << "hello...abc123" << endl;
  7.         fout << "hello...你好" << endl;
  8.         fout.close();     // 关闭流文件

  9.         // 输入文件流
  10.         ifstream fin(fname);  // 建立一个输入流对象  和文件关联
  11.         char ch;
  12.         while (fin.get(ch))  // 从文件获取一个字符
  13.         {
  14.                 cout << ch << " ";
  15.         }
  16.         fin.close();

  17.         system("pause");
  18.         return 0;
  19. }
复制代码

二进制文件读写:
  1. int main()
  2. {
  3.         // 输出流
  4.         char *fname = "d:/1.dat";
  5.         ofstream fout(fname, ios::binary);
  6.         if (!fout)
  7.         {
  8.                 cout << "打开文件失败" << endl;
  9.                 return;
  10.         }
  11.         Teacher t1(12, "小明");
  12.         Teacher t2(18, "小华");
  13.         fout.write((char*)&t1, sizeof(Teacher));
  14.         fout.write((char*)&t2, sizeof(Teacher));
  15.         fout.close();

  16.         // 输出流
  17.         ifstream fin(fname);
  18.         Teacher tmp;
  19.         fin.read((char*)&tmp, sizeof(Teacher));
  20.         cout << tmp.age << tmp.name << endl;

  21.         fin.read((char*)&tmp, sizeof(Teacher));
  22.         cout << tmp.age << tmp.name << endl;
  23.         fin.close();

  24.         system("pause");
  25.         return 0;
  26. }
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 02:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表