{I+=codeWee;}
[C++] How to Define operator << to Save file (sample code)
#include <stdio.h>
#include <wchar.h>

class BinaryFile
{
public:
	BinaryFile();
	virtual ~BinaryFile();

	BOOL OpenFile(const WCHAR* szFileName);
	void CloseFile();

	BOOL Write(const long* value, int nCount = 1);
	BinaryFile& operator <<(const long value); // <-----------------------


protected:
	FILE* m_pFile; 
};




BinaryFile::BinaryFile()
{
	m_pFile = NULL;
}


BinaryFile::~BinaryFile()
{
	CloseFile();
}

BOOL BinaryFile::OpenFile(const WCHAR* szFileName)
{
	_wfopen_s(&m_pFile, szFileName, L"wb");
	if (m_pFile == NULL)
	{
		return FALSE;
	}

	return TRUE;

}


void BinaryFile::CloseFile()
{
	if (m_pFile)
		fclose(m_pFile);
}


BOOL BinaryFile::Write(const long* value, int nCount/*=1*/)
{
	if (nCount < 1)
	{
		throw (L"nCount < 1!");
		return FALSE;
	}

	if (fwrite(value, sizeof(long), nCount, m_pFile) != 1)
	{
		wprintf(L"fwrite failed!\n");
		return FALSE;
	}
	return TRUE;
}

BinaryFile& BinaryFile::operator <<(const long value)
{
	try
	{
		Write(&value);
	}
	catch (WCHAR* msg)
	{
		throw msg;
	}
	return *this;
}
HTML | PHP | C++ | DirectX11 | Javascript | C# | HTML5 | ASP | SQL | General | CSS | Oculus Rift | Unity3d | Virtools SDK | Tip | NSIS | PowerShell | node.js | Web API | RTSP | All
Copyright© 2016 CodeWee.com All rights reserved.