搞懂C++文件流, 文件读写,fstream、seekg、seekp等的使用。

彻底搞懂C++文件流。
首先需要头文件#include< fstream >
fstream可以把它理解成一个父类,包含的子类有ifstream和ofstream等,
所以一般直接创建一个父类对象即可(即fstream类的对象)

下边的代码讲解了
1:4种文件的读写操作
2:如何使用get(),put(),getline(),write()以及read()函数。
3:如何得到文件的大小。
4:seekg()和teelg()配套使用的真谛,seekp()和teelp()同理

//文件流;
#include<iostream>
#include<fstream>  //ifstream  ofstream
using namespace std;
/*
	No1.文件流流类
	ofstream:打开文件,写文件
	ifstream:打开文件,读操作
	fstream:可读可写
	No2.一般用包含#include<fstream>
	No3.打开文件,关闭文件
	打开文件:
	void open(const char* URL,ios::openmode mode);
	mode: 
		ios:in  读的方式打开文件
		ios:out 写的方式打开文件
		ios::app 追加的方式写文件
		ios::ate  在已有的文件,文件指针在文件末尾
		ios::trunc  文件不存在,创建文件
		ios::binary  二进制形式打开文件,默认方式ASCII码方式打开
		ios::nocreate  不创建的方式
		ios::norepalce  不替换
	组合方式:用的是位或
		ios::in|ios::out 可读写
		ios::out|ios::binary 二进制写的方式打开文件,
	判断文件是否打开成功:
		1.is_open() 判断打开是否成功
		2.!文件对象  判断文件是否打开成佛那个
	关闭文件:close()成员函数关闭;
	*/
void testOpenFile() {    //用于测试用的函数
	//C++中打开文件是文件对象,不是文件指针。

	fstream File,File1;
	File.open("2.txt", ios::in | ios::out );
	File1.open("3.txt", ios::in | ios::out | ios::trunc);
	//使用open函数和直接用构造函数是一样的效果。
	if (!File.is_open()) {
		cerr << "2.txt 打开文件失败!!\n";
	}
	if (!File) {
		cerr << "2.txt打开文件失败!!!\n";
	}//这个if语句和上一个if语句作用相同
	if (!File1.is_open()) {
		cerr << "3.txt打开文件失败!!\n";
	}
	else {
		cout << "3.txt 打开成功!!!\n";
	}
	File.close();
	File1.close();
}
//No2.读写方式:
void asciiReadWriteFile(const char* readFileName, const char* writeFileName) {
	fstream read(readFileName, ios::in);
	fstream write(writeFileName, ios::out);
	//第一种读写方式:ASCII  直接流的方式读写, >>  <<
	/*while (1) {
		char temp;
		read >> temp;
		if (read.eof()) {
			break;
		}
		write << temp;
	}*/
	//第二种方式读写方式:可以保留空格和换行,用get()和put();
	/*while (1) {
		char temp;
		read.get(temp);
		//temp=read.get();一样的效果!
		if (read.eof()) {
			break;
		}
		write.put(temp);
	}*/
	//第三种读写方式:不能保留空格和换行,用getline()和write().
	char str[40]; 
	while (!read.eof()) {
		memset(str, ' ', 40);
		read.getline(str,40);
		int len = strlen(str);
		write.write(str,len);
		write.put('\n');
	}
}
//使用二进制读写方式  使用read()和write()函数来实现
void binaryReadWriteFile(const char* readFile, const char* writeFile) {
	fstream read(readFile, ios::in | ios::binary);
	fstream write(writeFile, ios::out | ios::binary);
	while (!read.eof()) {
		char str[1024]="";
		read.read(str, 1024);
		
		write.write(str, 1024);
	}
	read.close();
	write.close();
}
//
void testSeekReadFile(const char* fileName) {
	fstream fread(fileName, ios::in);
	fstream fwrite(fileName, ios::app|ios::out);
	if (!fread) {
		cerr << "打开文件\"fileName\"!失败!";
	}
	int f = fread.tellg();
	fread.seekg(0,ios::end);
	int l = fread.tellg();
	cout << "size  of  " << fileName << "is "<<(l - f) << "bytes.";
	fread.seekg(5);//表示将指针位置设置到绝对位置5;
	cout << (char)fread.get() << endl;
	fread.seekg(-1, ios::cur);		//seekg的第一个参数为负,表示从当前位置往前移
	cout << (char)fread.get() << endl;
	fread.seekg(1, ios::cur);//第一个参数为正,表示从当前位置往后数的第一个位置。
	
	fwrite.put('w');//在末尾追加写。
}
int main() {
	testOpenFile();
	asciiReadWriteFile("read.txt","write.txt");
	binaryReadWriteFile("read.txt", "binaryTest.txt");
	testSeekReadFile("read.txt");
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值