iniファイルを読出す場合は、文字列として読み出す場合はGetPrivateProfileString()関数を使用します。 数値として読み出す場合は、GetPrivateProfileInt()関数を使用します。
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
/*
iniファイルを読み込む
*/
int _tmain
(
int argc
, _TCHAR* argv[]
)
{
/*
std::wcoutのロケールを設定
これを設定するだけで、std::wcoutで日本語が表示される
ようになります。
*/
std::wcout.imbue( std::locale( "", std::locale::ctype ) );
// iniファイルパス
std::wstring strInfilePath = L".¥¥TestUnicode.ini";
/*
iniファイル読み込み(文字列)
*/
TCHAR waBuf[ MAX_PATH ];
if ( 0 == ::GetPrivateProfileString( L"SECTION", L"key_str", L"デフォルト文字列", waBuf, _countof( waBuf ), strInfilePath.c_str() ) ) {
std::wcout << L"iniファイルの読み込みに失敗しました。" << std::endl;
}
else {
// 読み込んだ値の表示
std::wcout << waBuf << std::endl;
}
/*
iniファイル読み込み(数値)
*/
int nValue = ::GetPrivateProfileInt( L"SECTION", L"key_value", 4567, strInfilePath.c_str() );
// 読み込んだ値の表示
std::wcout << nValue << std::endl;
// 正常終了
return( 0 );
}
テスト文字列 1234