c++文件 读写类

//这是一个自己写的关于读写操作的类,思路:先初始化路径,将数组长度写进文件头,然后再写文件主内容。这个类是用于哈夫曼编码,同时也可以用于其他

//以后只需将这个类实例化,就不用每次都要写那些读写操作,那些操作的代码两天不用就会忘记了~
//制作者:百变星狼_wolf杰
//日期:2012.12.6
#define MAX 256


template <class T>
class fileOperate
{
private:
 char path[MAX];//路径
 //T typearray;//操作数组
 //int len;//数组长度
 int temp[2];//缓存数组,储存长度
public:
 fileOperate(){}//默认构造函数
 fileOperate(const char way[]);//传值函数,传入path
 ~fileOperate(){}//默认析构函数
 void writeIn(T w[],int len);//写入函数
 void readFrom(T w[],int &testLen);//读出函数
};


template<class T>
fileOperate<T>::fileOperate(const char way[])
{
 strcpy(path,way);//初始化文件路径
};


template<class T>
void fileOperate<T>::writeIn(T w[],int len)
{
 FILE *file = fopen(path ,"w");
 temp[0] = len;
 fwrite(temp,sizeof(int),1,file);//将长度写进文件头
 fwrite(w,sizeof(T),len,file);//写进主要内容
 fclose(file);
};

template<class T>
void fileOperate<T>::readFrom(T w[],int &testLen)//读出函数
{
 FILE *file = fopen(path,"r");
 fread(temp,sizeof(int),1,file);
 int len = temp[0];
 fread(w,sizeof(T),len,file);//读主体内容
 testLen = len;//返回数组长度
 fclose(file);
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值