わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。

ファイルに書き込む

ifstreamのwriteメソッドファイルを読み込みます。

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <fstream>



/*
	ファイルの書き込み
*/
int _tmain
(
	  int argc
	, _TCHAR* argv[]
)
{
	// 標準出力にユニコード出力する
	setlocale( LC_ALL, "Japanese" );

	/*
		ファイルの書き込み
	*/
	{
		std::string strFile = "ファイルに書き込みます。";

		// ファイルのオープン
		std::ofstream ofstr( L"file.txt", std::ios::out | std::ios::binary | std::ios_base::trunc );
		if ( !ofstr ) {

			// ファイルがオープンできなかった
			std::wcout << L"ファイルオープン失敗" << std::endl;
			return( - 1);
		}

		// ファイルに書き込む
		ofstr.write( strFile.c_str(), strFile.size() );
	}

	// 正常終了
	return( 0 );
}


実行結果







わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。