わびさびサンプルソース

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

iniファイルを読み込む

iniファイルを読出す場合は、文字列として読み出す場合はGetPrivateProfileString()関数を使用します。 数値として読み出す場合は、GetPrivateProfileInt()関数を使用します。

iniファイルを読み込む
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#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






わびさびサンプルソース

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