现在的位置: 首页 > 综合 > 正文

C++文件流

2013年01月15日 ⁄ 综合 ⁄ 共 2813字 ⁄ 字号 评论关闭

1.stream,C++中所有的I/O流都以些“类为基础,包括文件标准IOI/O(fstream, ifstream, ofstream),字符串IOstringstream, istringstream, ostringstream

2.打开和关闭。

1Fstream fio("filename");

或 fstream fio; fio.open("filename", fstream::out | fstream::app, 0);

(1)文件模式。即open函数的第二个参数设置,

In: 读,定位文件头,不清空。

Out: 写,定位文件尾,清空文件数据。

App: 写时定位到文件尾,不清空。

Ate: 打开时定位文件尾,不清空。

Trunc: 打开时清空。

Binary: 二进制读写,

 同时以in|out模式打开进不清空,即fstream的默认模式。

(2)fio.close(); 

如需再用,必须先close(), clear(),然后用open()打开另一文件。

3.读写文件

(1)文本文件。

读“>>”,写“<<”。

=>写时会把换行符"\n"(十进制为10)自动扩充为回车加换行符(十进制为1310),所以当写入的是整数10时也会被转成1310. (以二进制方式读写时无此问题)

=>">>"的格式化输入。相关头文件<iomanip.h>

操纵符  功能  输入/输出

dec  格式化为十进制数值数据  输入和输出  

endl  输出一个换行符并刷新此流  输出  

ends  输出一个空字符  输出

hex  格式化为十六进制数值数据  输入和输出  

Oct  格式化为八进制数值数据  输入和输出  

setprecision(int p) 设置浮点数的精度位数  输出

(2)二进制文件。

ofstream &put(char ch) 写入一个字符。

ifstream &get(char &ch); 读取一个字府;

int get(); 读取返回一个字符,如到达文件尾则返回EOF

ifstream &get(char *buf,int num,char delim='n') 

读入了 num 个字符或遇到了由delim指定的字符

read(unsigned char *buf,int num); 读取数据块。可用gcount()获实际读取字符数;

write(const unsigned char *buf,int num);  写入数据块。

(3)检测文件尾。

成员函数eof()。如 if(in.eof() ) ...

int _tmain(int argc, _TCHAR* argv[])
{
	//写
	ofstream fout("out1.txt");
	if (!fout)
	{	cout<<"error: open write file out1.txt fail!"<<endl;
		return 1;
	}
	fout<<"write to out1.txt!"<<endl;
	fout<<"这是第二行,     第二行!"<<endl;
	int nnn = 10;
	fout<<nnn<<endl;
	fout.flush();	//清除缓存,立即写到文件
	fout.close();
	fout.clear();
	fout.open("out2.txt", ofstream::out| ofstream::binary);
	if (!fout)
	{	cout<<"error: open write file out2.txt fail!"<<endl;
		return 1;
	}
	string str = "write to out2.txt as binary mode!\n";
	fout.write(str.c_str(), str.size() );
	str = "这是第二行,  第二行!";
	fout.write(str.c_str(), str.size() );
	fout.flush();
	fout.close();
	fout.clear();
	//读
	ifstream fin("out1.txt");
	if (!fin)
	{	cout<<"error: open read file out1.txt fail!"<<endl;
		return 1;
	}
	while(!fin.eof() )
	{	string strRead;
		fin>>strRead;
		cout<<strRead<<endl;
	}
	fin.close();
	fin.clear();
	fin.open("out2.txt", ofstream::out|ifstream::binary);
	if (!fin)
	{	cout<<"error: open read file out2.txt fail!"<<endl;
		return 1;
	}
	char line[100];
	memset(line, 0, 100);
	fin.getline(line, 100);
	cout<<line<<endl;
	while(!fin.eof() )
	{	char bufRead[100] = {0};
		fin.read(bufRead, 100);
		cout.write(bufRead, strlen(bufRead) );
// 		string strRead;
// 		fin>>strRead;
// 		cout<<strRead<<endl;
	}
	//
// 	ifstream fin1("abc.txt");
// 	if (!fin1)
// 	{	cout<<"error: open read file abc.txt fail!"<<endl;
// 		return 1;
// 	}
	return 0;
}


4.文件定位。

文件流维护了两个指针:读指针和写指针。

istream &seekg(streamoff offset,seek_dir origin); 定位读指针。

ostream &seekp(streamoff offset,seek_dir origin); 定位写指针。

参数:offset :基于orgin的偏移量(字节)

Orgin :基准位置。可取 ios::beg 文件头, ios::cur 当前位置, ios::end 文件尾。

5.流对象的传递。

Stream基类的赋值构造函数声明为非public所以作为函数参数时使用引用或者指针传递,而非值传递。

例如: void fn(stream in);//

void fn(stream& in); //正确

 

6常用情况记录

(1)检测文件是否存在但不创建。

以写入模式打开文件时,文件不存在则会创建。只以读取模式打开时,文件不存在并不会创建,只是流出错。

代码:Ifstream fin("in.txt"); if(! fin) cout<<"打开文件in.txt失败";

(2)以空格或换行符为结束符输入。 “>>”操作符

(3)以换行符为结束符输入。 getline();

(4)以文件尾为结束符输入。 read();

(5)以指定字符作为结束符输入。 Get(char*s, streamsize n, char delim);


抱歉!评论已关闭.