《C++面向对象程序设计(第4版)》学习笔记-10

在这里插入图片描述

此份笔记建议在完整阅读郑莉老师、董渊老师、何江舟老师所编写的《C++语言程序设计(第4版)》后食用,风味更佳!
最后,由于本人水平有限,笔记中仍存在错误但还没有被检查出来的地方,欢迎大家批评与指正。


第11章 流类库与输入输出

11.1 I/O流的概念

  • 当程序与外界环境进行信息交换时,存在着两个对象,一个是程序中的对象,另一个是文件对象。
  • 流是一种抽象,它负责在数据的生产者和数据的消费者之间建立联系,并管理数据的流动。
  • 程序建立一个流对象,并指定这个流对象与某个文件对象建立连接,程序操作流对象,流对象通过文件系统对所连接的文件对象产生作用。
  • 读操作在流数据抽象中被称为(从流中)提取,写操作被称为(向流中)插入。

在这里插入图片描述


11.2 输出流

1.最重要的三个输出流
  • ostream
  • ofstream
  • ostringstream
2.预先定义的输出流对象
  • cout 标准输出
  • cerr 标准错误输出,没有缓冲,发送给它的内容立即被输出。
  • clog 类似于cerr,但是有缓冲,缓冲区满时被输出。
3.插入运算符
  • 插入(<<)运算符是所有标准C++数据类型预先设计的。
  • 用于传送字节到一个输出流对象。
4.控制输出格式

(1)控制输出宽度

  • 为了调整输出,可以通过在流中放入setw操纵符或调用width成员函数为每个项指定输出宽度。

(2)使用width控制输出宽度

int main()   
{
	double values[] = {1.23,35.36,653.7,4358.24};
	for(int i=0;i<4;i++)
	{
        cout.width(10);
        cout << values[i] <<'\n';
	}
}
//输出结果
      1.23
     35.36
     653.7
   4358.24

(3)使用*填充

#include <iostream>  
using namespace std; 
int main()   
{ 
	double values[]={1.23,35.36,653.7,4358.24};
	for(int i=0; i<4; i++)
	{  
		cout.width(10);
        cout.fill('*');
     	cout<<values[i]<<'\n';   
	}
}
//输出结果:
******1.23
*****35.36
*****653.7
***4358.24

(4)使用setw指定宽度

#include <iomanip>   
int main()   
{
	double values[]={1.23,35.36,653.7,4358.24};
  	char *names[]={"Zoot","Jimmy","Al","Stan"};
	for(int i=0;i<4;i++)
		cout<<setw(6)<<names[i]<<setw(10)<<values[i]<<endl;   
}
//输出结果
  Zoot      1.23
 Jimmy     35.36
    Al     653.7
  Stan   4358.24

(5)设置对齐方式

#include <iomanip>  
int main()   
{
	double values[]={1.23,35.36,653.7,4358.24};
    char *names[]={"Zoot","Jimmy","Al","Stan"};
    for(int i=0;i<4;i++)
    {
        cout<<setiosflags(ios::left)
        <<setw(6)<<names[i]
        <<resetiosflags(ios::left)
        <<setw(10)<<values[i]
        <<endl;
    }    
}
//输出结果
Zoot        1.23
Jimmy      35.36
Al         653.7
Stan     4358.24

(6)控制输出精度

#include <iomanip> 
int main()   
{ 
	double values[]={1.23,35.36,653.7,4358.24};
    char *names[]={"Zoot","Jimmy","Al","Stan"};
    cout<<setiosflags(ios::scientific);
    for(int i=0;i<4;i++)
    {
        cout<<setiosflags(ios::left)
        <<setw(6)<<names[i]
        <<resetiosflags(ios::left)
        <<setw(10)<<setprecision(1)
        << values[i]<<endl;
    }   
}
//输出结果
Zoot           1
Jimmy     4e+001
Al        7e+002
Stan      4e+003

(7)进制

  • dec、oct和hex操纵符设置输入和输出的缺省进制。
5.输出文件流成员函数

(1)输出流成员函数有三种类型:

  • 与操纵符等价的成员函数。
  • 执行非格式化写操作的成员函数。
  • 其它修改流状态且不同于操纵符或插入运算符的成员函数。

(2)函数

  • open函数
    • 把流与一个特定的磁盘文件关联起来。
    • 需要指定打开模式。
  • put函数
    • 把一个字符写到输出流中。
  • write函数
    • 把内存中的一块内容写到一个输出文件流中
  • seekp和tellp函数
    • 操作文件流的内部指针
  • close函数
    • 关闭与一个输出文件流关联的磁盘文件
  • 错误处理函数
    • 在写到一个流时进行错误处理

(3)向文件输出

#include <fstream>
using namespace std;
struct Date
{     
    int mo,da,yr;  
};

int main()  
{
    Date dt = {6,10,92};
    ofstream tfile("date.dat",ios::binary);
    tfile.write((char *) &dt,sizeof(dt));
    tfile.close();
}

(4)二进制输出文件

  • 以通常方式构造一个流,然后使用setmode成员函数,在文件打开后改变模式。
  • 使用ofstream构造函数中的模式参量指定二进制输出模式
  • 使用二进制操作符代替setmode成员函数:ofs << binary;

11.3 输入流

1.重要的输入流类
  • istream类最适合用于顺序文本模式输入。cin是其派生类istream_withassign的对象。
  • ifstream类支持磁盘文件输入。
  • istringstream
2.提取运算符
  • 提取运算符(>>)对于所有标准C++数据类型都是预先设计好的。
  • 是从一个输入流对象获取字节最容易的方法。
  • ios类中的很多操纵符都可以应用于输入流。但是只有少数几个对输入流对象具有实际影响,其中最重要的是进制操纵符dec、oct和hex。
3.输入流成员函数
  • open函数把该流与一个特定磁盘文件相关联。
  • get函数的功能与提取运算符(>>)很相像,主要的不同点是get函数在读入数据时包括空白字符。
  • getline的功能是从输入流中读取多个字符,并且允许指定输入终止字符,读取完成后,从读取的内容中删除终止字符。
  • read成员函数从一个文件读字节到一个指定的内存区域,由长度参数确定要读的字节数。
    如果给出长度参数,当遇到文件结束或者在文本模式文件中遇到文件结束标记字符时结束读取。
  • seekg函数用来设置输入文件流中读取数据位置的指针。
  • tellg函数返回当前文件读指针的位置。
  • close函数关闭与一个输入文件流关联的磁盘文件。
4.设置位置指针
#include <iostream>
#include <fstream>
using namespace std;
void main()
{ 
    char ch;
    ifstream tfile("payroll",ios::binary|ios::nocreate);
    if(tfile)
    { 
        tfile.seekg(8);
     	while(tfile.good()) 
     	{ 
            tfile.get(ch);
      		if (!ch) 
                break; 
            cout<<ch; 
        }
    }
    else 
    { 
        cout<<"ERROR: Cannot open file 'payroll'."<<endl; 
    }
    tfile.close();
}
5.文件读二进制记录
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
void main()
{ 
    struct
    { 
        double salary;
     	char name[23];
    } 
    employee;
    ifstream is("payroll",ios::binary|ios::nocreate);
    if (is)
    { 
        is.read((char *) &employee,sizeof(employee));
     	cout<<employee.name<<' '<<employee.salary<<endl;
    }
    else 
    { 
        cout<<"ERROR: Cannot open file 'payroll'."<<endl;
    }
    is.close();
}
6.读文件并显示其中空格的位置
#include <iostream>
#include <fstream>
using namespace std;
void main()
{ 
    char ch;
    ifstream tfile("payroll",ios::binary|ios::nocreate);
    if(tfile) 
    {
        while(tfile.good()) 
        {
            streampos here = tfile.tellg();
         	tfile.get(ch);
         	if(ch==' ') 
                cout<<"\nPosition "<< here<<" is a space";
        }
    }
    else
    { 
        cout<<"ERROR: Cannot open file 'payroll'."<<endl;
    }
    tfile.close();
}

11.4 输入/输出流

猜你喜欢

转载自blog.csdn.net/Jason3633/article/details/91965911