文件写入:
#ifndef _CFILEWRITE_H_
#define _CFILEWRITE_H_
//文件写入类
class CFileWrite
{
char* p;
int len;
int size;
void Kuorong(int byte);
public:
CFileWrite();
~CFileWrite();
void Save(const char* filename);//保存文件
void Write(int a);
void Write(char a);
void Write(short a);
void Write(double a);
void Write(float a);
void Write(long a);
void Write(unsigned int a);
void Write(unsigned short a);
void Write(unsigned char a);
void Write(void* mem,int len);//写一段内存
void Write(const char* a);//写字符串
};
#endif
#include "CFileWrite.h"
#include <iostream>
CFileWrite::CFileWrite()
{
size = 128;
len = 0;
p = new char[size];
}
CFileWrite::~CFileWrite()
{
if (p)
delete [] p;
}
void CFileWrite::Save(const char* filename)
{
FILE* fp;
fopen_s(&fp, filename, "wb");
fwrite(p, len, 1, fp);
fclose(fp);
}
void CFileWrite::Kuorong(int byte)
{
if (len + byte > size)
{
//计算新的容量
int newsize = size * 2;
while (len + byte > newsize)
newsize *= 2;
char* np = new char[newsize];
memcpy(np, p, len);
delete [] p;
p = np;
size = newsize;
}
}
void CFileWrite::Write(int a)
{
Kuorong(sizeof(a));
//memcpy(p+len, &a, sizeof(a));
*(int*)(p+len) = a;
len += sizeof(a);
}
void CFileWrite::Write(char a)
{
Kuorong(sizeof(a));
memcpy(p+len, &a, sizeof(a));
len += sizeof(a);
}
void CFileWrite::Write(short a)
{
Kuorong(sizeof(a));
memcpy(p+len, &a, sizeof(a));
len += sizeof(a);
}
void CFileWrite::Write(double a)
{
Kuorong(sizeof(a));
memcpy(p+len, &a, sizeof(a));
len += sizeof(a);
}
void CFileWrite::Write(float a)
{
Kuorong(sizeof(a));
memcpy(p+len, &a, sizeof(a));
len += sizeof(a);
}
void CFileWrite::Write(long a)
{
Kuorong(sizeof(a));
memcpy(p+len, &a, sizeof(a));
len += sizeof(a);
}
void CFileWrite::Write(unsigned int a)
{
Kuorong(sizeof(a));
memcpy(p+len, &a, sizeof(a));
len += sizeof(a);
}
void CFileWrite::Write(unsigned short a)
{
Kuorong(sizeof(a));
memcpy(p+len, &a, sizeof(a));
len += sizeof(a);
}
void CFileWrite::Write(unsigned char a)
{
Kuorong(sizeof(a));
memcpy(p+len, &a, sizeof(a));
len += sizeof(a);
}
void CFileWrite::Write(void* mem,int len)
{
Kuorong(len);
memcpy(p+CFileWrite::len, mem, len);
CFileWrite::len += len;
}
void CFileWrite::Write(const char* a)
{
int r = strlen(a) + 1;
Kuorong(r);
memcpy(p+len, a, r);
len += r;
}
//文件读取
#ifndef _CFILEREAD_H_
#define _CFILEREAD_H_
//文件读取类
class CFileRead
{
char* p;//所有数据的首地址
int size;//文件总大小
int len;//读取到什么位置了
public:
CFileRead(const char* filename);
~CFileRead();
void SetPos(int pos);//设置位置
bool Read(int* a);//读取一个整数
bool Read(short* a);//读取一个short
bool Read(float* a);//读取一个float
bool Read(double* a);//读取一个double
bool Read(char* a);//读取一个char
bool Read(bool* a);//读取一个bool
bool Read(long* a);//读取一个long
bool ReadString(char* a);//读取一个字符串
bool ReadMem(void* a,int b);//读取b长度的数据出来
};
#endif
#include "CFileRead.h"
#include <iostream>
CFileRead::CFileRead(const char* filename)
{
p = 0;
size = 0;
len = 0;
FILE* fp;
fopen_s(&fp, filename, "rb");
if (fp == 0)
return;
fseek(fp, 0 ,SEEK_END);
size = ftell(fp);
fseek(fp, 0 ,SEEK_SET);
p = new char[size];
fread(p, size, 1, fp);
fclose(fp);
}
CFileRead::~CFileRead()
{
if (p)
delete [] p;
}
void CFileRead::SetPos(int pos)//设置位置
{
if (pos < 0)
len = 0;
else
len = pos;
}
bool CFileRead::Read(int* a)//读取一个整数
{
if (len + sizeof(int) > size)
return false;
*a = *(int*)(p+len);
len += sizeof(int);
return true;
}
bool CFileRead::Read(short* a)//读取一个short
{
return ReadMem(a,2);
}
bool CFileRead::Read(float* a)//读取一个float
{
return ReadMem(a,4);
}
bool CFileRead::Read(double* a)//读取一个double
{
return ReadMem(a,8);
}
bool CFileRead::Read(char* a)//读取一个char
{
return ReadMem(a,1);
}
bool CFileRead::Read(bool* a)//读取一个bool
{
return ReadMem(a,1);
}
bool CFileRead::Read(long* a)//读取一个long
{
return ReadMem(a,4);
}
bool CFileRead::ReadString(char* a)//读取一个字符串
{
if (len >= size)
return false;
strcpy(a, p+len);
len += strlen(p+len)+1;
return true;
}
bool CFileRead::ReadMem(void* a,int b)//读取b长度的数据出来
{
if (len + b > size)
return false;
memcpy(a, p+len, b);
len += b;
return true;
}
//函数调用一下试试
#include <iostream>
#include "CFileRead.h"
#include "CFileWrite.h"
void main()
{
CFileWrite g;
g.Write(100);//写入int
g.Write(100.45);//写入double
g.Write("你好世界");
for (int i = 0;i < 10; ++i)
g.Write(i);//写入10个整数
g.Save("abc");
//--------------------------------------
CFileRead f("abc");
int a;
f.Read(&a);//读取一个int数据
double b;
f.Read(&b);//读取一个double数据
char s[128];
f.ReadString(s);
int arr[10];
f.ReadMem(arr, 40);//读取10个整数
std::cout<< a <<std::endl;
std::cout<< b <<std::endl;
std::cout<< s <<std::endl;
for (int i = 0;i < 10; ++i)
std::cout<< arr[i] <<std::endl;
}