C++ I/O流

转载

I/O系统的任务就是在内存和外部设备之间稳定可靠地传输数据和解释数据。

流类库

  1. streambuf提供对缓冲区的低级操作,ios提供流的高级I/O操作
  2. ios派生了两个类,输入流类istream和输出流类ostream
基类派生类功能
streambuffilebuf提供文件缓冲区的管理
strstreambuf使用字符串保存字符序列,提供在内存中提取和插入操作的缓冲区管理
stdiobuf提供标准I/O文件的缓冲区管理
istreamifstream文件输入流类,用于对文件的提取操作
istrstream字符串输入流类,用于对字符串的提取操作
istream_withassign重载了赋值运算符的输入流类,标准输入流cin是该类的对象
ostreamofstream文件输出流类
ostrstream输出流类
ostream_withassign重载了赋值运算符的输出流类,标准输出流cout、cerr(非缓冲错误输出流)、clog(缓冲错误输出流)是该类对象

标准流

名称含义连向位置
cin标准输入流默认连向键盘
cout标准输出流默认连向显示器
cerr标准错误输出流连向显示器
clog标准错误输出流连向打印机

输入流操作

函数功能
read无格式输入指定字节数
get从流中提取字符,包括空格
getline从流中提取一行字符
ignore提取并丢弃流中指定字符
peek返回流中下一个字符,但不从流中删除
gcount统计最后输入的字符个数
eatwhite忽略前导空格
seekg移动输入流指针
tellg返回输入流中指定位置的指针值
operator>>提取运算符
#inclued <iostream>
using namespace std;
int main(){
	char c;
	while((c=cin.get()!='\n'))//读取一行
		cout.put(c);
	while(cin.get(c)){//读取一行
		if(c=='\n')	break;
		cout.put(c);
	char s[80];
	cin.get(s,10);//读取9个字符
	cout<<s<<endl;
}

   
   

输出流操作

函数功能
put插入一个字节
write插入一个字节序列
flush刷新输出流
seekp移动输出流指针
tellp返回输出流中指定位置的指针值
operator<<插入运算符
ofstream outf;
double x=0.618;
outf.write((char*)&x,8);//把变量x的8字节数据写入输出流当前位置

char s[80];
cin.get(s,10);
cout<<write(s,10);

串流

#include<iostream>
#include<sstream>
#include<string>
using namespace std;
int main(){
	string testStr("Input test 256*0.5");
	string s1,s2,s3;
	double x,y;
	istringstream input(testStr);//建立istringstream类对象,与串对象连接
	input>>s1>>s2>>x>>s3>>y;//通过input从testStr中提取数据
	cout<<s1<<s2<<x<<s3<<y;
}

 
 
#include<iostream>
#include<sstream>
using namespace std;
int main(){
	ostringstream Output;
	double x,y;
	cin>>x;
	cin>>y;
	Output<<x<<y;//插入数据项
	cout<<Output.str();
}

 
 

文件处理

三个基本步骤:

  1. 打开文件
  2. 读/写文件
  3. 关闭文件

文件打开

//打开一个已有文件
ifstream infile;//建立输入文件流对象
infile.open("dtafile.dat",ios::in);//连接文件,指定打开方式
//创建一个文件
ofstream outfile;//建立输入文件流对象
outfile.open("d:\\newfile.dat",ios::out);//连接文件,指定打开方式

ifstream infile(“datafile.dat”,ios::in);
ofstream outfile(“d:\newfile.dat”,ios::out)
fstream rwfile(“myfile.dat”,ios::in|ios::out);

文件打开方式

标识常量意义
ios::in读方式打开文件
ios::out写方式打开文件
ios::ate打开文件时,文件指针指向文件末尾
ios::app追加方式,将向文件中输出的内容追加到文件尾部
ios::trunc**删除文件现有内容**
ios::nocreate如果文件不存在,则打开操作失败
ios::noreplace如果文件存在,则打开操作失败
ios::binary二进制代码方式打开,默认为文本方式

关闭文件

关闭文件操作包括:把缓冲区数据完整地写入文件,添加文件结束标志,切断流对象与外部文件的连接。

ifstream infile;
infile.open("file1.txt",ios::in);
infile.close();
infile.open("file2.txt",ios::in);
//若流对象的生存期没有结束,即流对象依然存在,还可以与其他文件连接

 
 

文本文件

创建文件

#include <iostream>
#include <fstream>
using namespace std;
int main(){
	char fileName[30],name[30];
	int number,score;
	ofstream outsbuf;
	cin>>fileName;
	outsbuf.open(fileName,ios::out);//打开文件
	if(!outstuf){
		cerr<<"file could not be open"<<endl;
		abort();
	}
	outstuf<<"This is a file of students\n";//写入文件
	while(cin>>number>>name>>score)
		outstuf<<number<<name<<score<<'\n';
	outstuf.close();//关闭文件

 
 

读文件

#include <iostream>
#include <fstream>
using namespace std;
int main(){
	char name[30],s[80];
	int number,score;
	int n=0,max,min,total=0;
	double ave;
	ifstream instuf("d:\\student.txt",ios:in);
	instuf.seekg(0,ios::beg);//流指针置在文件头
	if(!instuf){
		cout<<"file could not be open."<<endl;
		abort();
	}
	instuf.getline(s,80);//略去标题行
	while(instuf>>number>>name>>score){
		cout<<number<<name<<score;
		if(==0)
			max=min=score;
		else{
			if(score>max) max=score;
			if(score<min) min=score;
		}
		total+=score;
		n++;
	}
	ave=double(total)/n;
	cout<<max<<min<<ave;
	instuf.close();
}

 
 

浏览文件

void browseFile(char *fileName,int delLine){
	ifstream inf(fileName,ios::in);
	char s[80];
	for(int i=1;i<=delLine;i++)//读出开头delLine行不显示
		inf.getline(s,80);
	while(!inf.eof()){//eof为文件结束符
		inf.getline(s,80);
		cout<<s<<endl;
	}
	inf.close();
}

 
 

追加记录

int Append(char *fileName){
	char name[30],ch;
	int number,scorel
	ofstream outstuf(fileName,los::app);
	if(!outstuf){
		cerr<<"file could not be open"<endl;
		return 0;
	}
	cout<<"Do you want to append(Y/N)?"<<endl;
	while(ch=='Y'||ch=='y'){
		cout<<"Input the number,name,and score:\n";
		cin>>number>>name>score;
		outfstuf<<number<<' '<<name<<' '<<score<<' '<<endl;//写入文件
		cout<<"Y/N?";
		cin>>ch;
		if(ch=='N'||ch=='n')
			cout<<"close file.\n";
	}
	outstuf.close();
	return 1;
}

 
 

复制文件

int copyFile(char *destFile,char *srcFile){
	char ch;
	ifstream infile(srcFile,ios::in);
	ofstream outfile(desFile,ios::out);
	if(!infile){
		cerr<<srcFile<<":file could not be open. "<<endl;
		return 0;
	}
	if(!outfile){
		cerr<<desFile<<":file could not be open. "<<endl;
		return 0;
	}
	while(infile.get(ch))//全部字符复制  get()  put()
		outfile.put(ch);
	infile.close();
	outfile.close();
	return 1;
}

 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值