本文代码来源于:https://blog.csdn.net/zy19940906/article/details/49659895
#pragma once
#ifndef _STRUCT_H_
#define _STRUCT_H_
#include<vector>
#include<iostream>
#include <string>
using namespace std;
#define MaxSize 10
#pragma pack(1)
typedef struct _TEST_DAT
{
int type;
//char name[MaxSize];
int age;
}TESTDAT;
#pragma pack()
#endif
#pragma once
#ifndef _TXT_TO_FILE_H_
#define _TXT_TO_FILE_H_
#include<vector>
#include<iostream>
#include<fstream>
#include<string>
#include"Struct.h"
#include<stdio.h>
using namespace std;
class TxtToFile
{
public:
TxtToFile();
~TxtToFile();
bool SaveToDat(string _datPath);//外调函数,保存到dat文件
bool GetFromDat(string _datPath);
private:
TESTDAT m_dat;
fstream m_datstream;
vector<TESTDAT>m_getdat;
private:
//把基本的Number类型写入文件流中
template<class T>
bool Write(T value)
{
m_datstream.write(reinterpret_cast<char *>(&value), sizeof(T));
return true;
}
//把字符串写入文件流
void WriteString(string _str, int _count);
__int64 getFileSize(const char *filename);
};
#endif
#include "TxtToFile.h"
#include <sstream>
TxtToFile::TxtToFile()
{
m_dat = TESTDAT{ 123, 345 };
}
TxtToFile::~TxtToFile()
{
}
bool TxtToFile::SaveToDat(string _datPath)
{
if (_datPath.size() == 0)
{
return false;
}
m_datstream.open(_datPath, fstream::out | fstream::binary);
if (!m_datstream.is_open())
{
cout << "open" << _datPath << "Failed" << endl;
m_datstream.close();
return false;
}
Write(m_dat.type);
//WriteString(m_dat.name, MaxSize);
Write(m_dat.age);
cout << "Successed" << endl;
m_datstream.close();
return true;
}
void TxtToFile::WriteString(string _str, int _count)
{
if ((int)_str.length() >= _count)
{
cout << "write string error!" << endl;
return;
}
m_datstream << _str;
int len = _count - _str.length();
cout << _count << endl;
cout << _str.length() << endl;
while (len--)
{
m_datstream << "\0";
}
}
bool TxtToFile::GetFromDat(string _datPath)
{
if (_datPath.size() == 0)
{
return false;
}
__int64 scount = 0;
__int64 stuctsize = sizeof(TESTDAT);
cout << stuctsize;
TESTDAT testdat;
__int64 filesize = getFileSize(_datPath.c_str());
cout << filesize;
//if (filesize == 0 || filesize < stuctsize || (filesize%stuctsize) != 0)
//{
// return false;
//}
//scount = filesize / stuctsize;
//cout << scount;
FILE *inStream = NULL;
const char *name = _datPath.c_str();
fopen_s(&inStream,name, "rb");
if (inStream == NULL)return false;
for (int i = 0; i < 1; i++)
{
fread(&testdat, stuctsize, 1, inStream);
m_getdat.push_back(testdat);
cout << testdat.age << " " << testdat.type << " " << endl;
}
fclose(inStream);
return true;
}
__int64 TxtToFile::getFileSize(const char *filename)
{
__int64 size = 0;
fstream instreams;
instreams.open(filename, ios::_Nocreate);
if (!instreams.is_open())
{
return 0;
}
instreams.seekg(0, ios::end);//移到最后
size = instreams.tellg();//告诉现在位置在哪里
instreams.close();
return size;
}
#include"TxtToFile.h"
int main()
{
TxtToFile ttofile;
ttofile.SaveToDat("Test.Dat");
ttofile.GetFromDat("Test.Dat");
getchar();
getchar();
return 0;
}
Dat文件是一个存取数据的文件,不管是什么数据我们都是将其转换成char型数据进行的存取,所以在读取出来时,我们必须严格按照存入的格式进行存取,比如我们存储数据为int double float那么我们就必须按照这三种格式进行存取,出现一个字节的偏移都会导致后面的结果全面出错。所以我们用记事本打开时dat文件时总是乱码的原因。