わびさびサンプルソース

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

指定したファイルへstringを書き込む

ofstreamへstringを出力します。

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



/*
	指定したファイルへstringを書き込む
*/
HRESULT WriteStringToFile
(
	  std::wstring oPath
	, std::string& oString
)
{
	std::ofstream oOfstream( oPath.c_str() );

	if ( oOfstream.is_open()) {

		// ファイルへ書き込む
		oOfstream << oString;

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

	// 失敗
	return( E_FAIL );
}



int _tmain
(
	  int argc
	, _TCHAR* argv[]
)
{
	// 標準出力にユニコード出力する
	setlocale( LC_ALL, "Japanese" );

	// 書き込む内容
	std::string oString = L"あいうえお"

	// wstringをstringに変換する
	WriteStringToFile( L"test_file.txt", oString );

	// 標準出力へ出力する
	std::cout << L"ファイルへ書き込みました。" << std::endl;

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

実行結果

ファイルへ書き込みました。






わびさびサンプルソース

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