C++文件读写


________________________________________
 
第十一章 流类库与输入/输出
王龙  主讲
C++语言程序设计
 

C++语言程序设计
本章主要内容
o I/O流的概念
o 输出流
o 输入流
o 输入/输出流
 

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

C++语言程序设计
输出流
? 最重要的三个输出流是
o ostream
o ofstream
o ostringstream
 

C++语言程序设计
输出流对象
? 预先定义的输出流对象:
o cout 标准输出
o cerr 标准错误输出,没有缓冲,发送给它的内容立即被输出。
o clog 类似于cerr,但是有缓冲,缓冲区满时被输出。
 
 
输出流
 

C++语言程序设计
输出流对象
? ofstream类支持磁盘文件输出
? 如果在构造函数中指定一个文件名,当构造这个文件时该文件是自动打开的
o ofstream myFile("filename",iosmode);
? 可以在调用默认构造函数之后使用open成员函数打开文件
ofstream myFile; //声明一个静态输出文件流对象
myFile.open("filename",iosmode);    
//打开文件,使流对象与文件建立联系
ofstream* pmyFile = new ofstream;     
//建立一个动态的输出文件流对象
pmyFile->open("filename",iosmode);    
//打开文件,使流对象与文件建立联系
输出流
 

C++语言程序设计
插入运算符(<<)
? 插入(<<)运算符是所有标准C++数据类型预先设计的。
? 用于传送字节到一个输出流对象。
 
输出流
 

C++语言程序设计
控制输出格式
? 控制输出宽度
o 为了调整输出,可以通过在流中放入setw操纵符或调用width成员函数为每个项指定输出宽度。
? 例11-1 使用width控制输出宽度
#include <iostream>  
using namesoace std;
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
 

C++语言程序设计
例:使用*填充
#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
 
10 
C++语言程序设计
例11-2使用setw指定宽度
#include <iostream>  
#include <iomanip>  
using namespace std;
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
 
11 
C++语言程序设计
例11-3设置对齐方式
#include <iostream>  
#include <iomanip>
using namespace std; 
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
 
 
12 
C++语言程序设计
例11-4控制输出精度
#include <iostream>  
#include <iomanip>
using namespace std; 
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
 
 
13 
C++语言程序设计
进制
dec、oct和hex操纵符设置输入和输出的缺省进制。
输出流
 
 
14 
C++语言程序设计
输出文件流成员函数
? 输出流成员函数有三种类型:
o 与操纵符等价的成员函数。
o 执行非格式化写操作的成员函数。
o 其它修改流状态且不同于操纵符或插入运算符的成员函数。
 
输出流
 
 
15 
C++语言程序设计
输出文件流成员函数
? open函数
把流与一个特定的磁盘文件关联起来。
需要指定打开模式。
? put函数
把一个字符写到输出流中。
? write函数
把内存中的一块内容写到一个输出文件流中
? seekp和tellp函数
操作文件流的内部指针
? close函数
关闭与一个输出文件流关联的磁盘文件
? 错误处理函数
在写到一个流时进行错误处理
输出流
 
 
16 
C++语言程序设计
例11-5向文件输出
#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();
}
输出流
 
 
17 
C++语言程序设计
二进制输出文件
? 以通常方式构造一个流,然后使用setmode成员函数,在文件打开后改变模式。
? 使用ofstream构造函数中的模式参量指定二进制输出模式
? 使用二进制操作符代替setmode成员函数:ofs << binary;
 
输出流
 
18 
C++语言程序设计
输入流
? 重要的输入流类:
o istream类最适合用于顺序文本模式输入 。
cin是其派生类istream_withassign的对象。
o ifstream类支持磁盘文件输入。
o istringstream
 
 
19 
C++语言程序设计
输入流对象
? 如果在构造函数中指定一个文件名,在构造该对象时该文件便自动打开。
ifstream myFile("filename",iosmode);
? 在调用缺省构造函数之后使用open函数来打开文件。
ifstream myFile;//建立一个文件流对象
myFile.open("filename",iosmode);     
          //打开文件"filename"
输入流
 
 
20 
C++语言程序设计
提取运算符(>>)
? 提取运算符(>>)对于所有标准C++数据类型都是预先设计好的。
? 是从一个输入流对象获取字节最容易的方法。
? ios类中的很多操纵符都可以应用于输入流。但是只有少数几个对输入流对象具有实际影响,其中最重要的是进制操纵符dec、oct和hex。
 
输入流
 
 
21 
C++语言程序设计
输入流成员函数
? open函数把该流与一个特定磁盘文件相关联。
? get函数的功能与提取运算符(>>)很相像,主要的不同点是get函数在读入数据时包括空白字符。(第6章介绍过)
? getline的功能是从输入流中读取多个字符,并且允许指定输入终止字符,读取完成后,从读取的内容中删除终止字符。(第6章介绍过)
? read成员函数从一个文件读字节到一个指定的内存区域,由长度参数确定要读的字节数。
如果给出长度参数,当遇到文件结束或者在文本模式文件中遇到文件结束标记字符时结束读取。
 
输入流
 
 
22 
C++语言程序设计
输入流成员函数
? seekg函数用来设置输入文件流中读取数据位置的指针。
? tellg函数返回当前文件读指针的位置。
? close函数关闭与一个输入文件流关联的磁盘文件。
 
输入流
 
 

23 
C++语言程序设计
例11-9 设置位置指针
#include <iostream>
#include <fstream>
using namespace std;
int 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();
}
输入流
 
24 
C++语言程序设计
例11-8 文件读二进制记录
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int 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();
}
输入流
 
25 
C++语言程序设计
例11-10 读文件并显示其中空格的位置
#include <iostream>
#include <fstream>
using namespace std;
int 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();
}
输入流

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值