当前位置:   article > 正文

struct和vector序列化保存数据_struct vector

struct vector

本文主要记录一下两点:
1、struct序列化保存
2、vector序列化保存

#include<iostream>
#include<Windows.h>
#include<vector>
#include<fstream>
#include<iterator>


using std::vector;
using std::cout;
using std::endl;
using std::ifstream;
using std::ofstream;


const int MAX_DATA = 500;

struct stInfo
{
	int nAge;
	char szName[20];
	char chSex;
	stInfo(int nAge,char* pName,char szSex)
	{
		this->nAge = nAge;
		strcpy_s(this->szName, pName);
		this->chSex = szSex;

	}
	stInfo()
	{
		memset(this, 0, sizeof(stInfo));
	}
	stInfo& operator=(const stInfo&para)
	{
		this->nAge = para.nAge;
		strcpy_s(this->szName, para.szName);
		this->chSex = para.chSex;
		return *this;
	}

	void Print()
	{
		cout << "Name:" << szName << "\tYear:" << nAge << "\tSex:" << chSex << endl;
	}
};

int main()
{
	//struct serize, save memory
	stInfo info(19, "helen", 'M');
	BYTE *pSrc(new BYTE[MAX_DATA]);
	ZeroMemory(pSrc, MAX_DATA);
	*(stInfo*)pSrc = info;
	//Write
	ofstream fout("1.txt", std::ios::out | std::ios::binary);
	if (fout.is_open())
	{
		fout.write((char*)pSrc, sizeof(stInfo));
		fout.close();
	}
	delete[]pSrc;
	pSrc = nullptr;


	//Read
	char szData[MAX_DATA] = { 0 };
	ifstream fin("1.txt", std::ios::binary);
	if (fin.is_open())
	{
		fin.read(szData, MAX_DATA);
		fin.close();
	}

	BYTE *pData = new BYTE[MAX_DATA];
	ZeroMemory((void*)pData, MAX_DATA);

	pData = (BYTE*)szData;
	stInfo infoRst;
	infoRst = *(stInfo*)pData;
	infoRst.Print();
	delete[]pData;
	pData = nullptr;



	//vector Serize
	vector<int>vecData;
	for (int nIndex = 0; nIndex < 5; ++nIndex)
		vecData.push_back(nIndex);
	//写入文件
	ofstream outFile("test.txt", std::ios::out | std::ios::binary);
	if (outFile.is_open())
	{
		copy(vecData.begin(), vecData.end(), std::ostream_iterator<int>(outFile," "));
		outFile.close();
	}
		
	//从文件读取
	vector<int>vecRst;
	ifstream inFile("test.txt", std::ios::binary);
	if (inFile.is_open())
	{
		copy(std::istream_iterator<int>(inFile), std::istream_iterator<int>(), back_inserter(vecRst));
		inFile.close();
	}
		
	for (auto&itData : vecRst)
		cout << itData << ',';
	cout << endl;
	//从文件读取直接输出
	copy(vecData.begin(), vecData.end(), std::ostream_iterator<int>(cout, " "));


	system("pause");
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/243978
推荐阅读
相关标签
  

闽ICP备14008679号