#include "fstream"

std::ofstream		fOutDataFile;	// 파일 생성 및 입력
std::ifstream		fInDataFile;	// 파일 읽어 오기

// 파일 생성
{
	// 바이너리 값을 입력 할 수 있도록 생성
	fOutDataFile.open( "DataFile.dat", std::ios_base::binary );

	if ( fOutDataFile.fail() )
	{
		return;
	}
}

// INT형 쓰기
{
	int		nValue;

	fOutDataFile.write( (const char*)nValue, sizeof(nValue) );
}

// String형 쓰기
{
	std::string strValue = "MonsterName";
	int			nSize	 = strValue.size();

	fOutDataFile.write( (const char*)nSize, sizeof(nSize) );
	fOutDataFile.write( strValue.c_str(), nSize );
}

// 파일 읽기
{
	int nOpenMode = std::ios_base::in || std::ios_base::binary;

	fInDataFile.open( "DataFile.dat", nOpenMode );

	if ( fInDataFile.fail() )
	{
		return;
	}
}

// INT형 읽기
{
	int		Value = 0;

	fInDataFile.read( (char*)&Value, sizeof(Value) );
}

// String형 읽기
{
	int			nSize		= 0;	
	char		Buf[512]	= "";
	std::string	strValue	= "";

	fInDataFile.read( (char*)&nSize, sizeof(nSize) );
	fInDataFile.read( Buf, nSize);

	strValue = Buf;
}

'Programming > C/C++' 카테고리의 다른 글

숫자 타입 String에 담기  (0) 2012.11.05
스마트 포인터 사용법 정리  (0) 2012.11.05
Map 사용 방법 정리  (0) 2012.11.05
Posted by Habentius
: