iniファイルへ書き込む場合は、WritePrivateProfileString()関数を使用します。 読み出しの場合は、GetPrivateProfileInt()関数が存在しますが、 書き込みの場合はWritePrivateProfileInt()関数は存在しません。数値を書き込みたい場合は、数値を文字列に変換してから WritePrivateProfileString()関数を呼び出してください。
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 56 57 58 59 | #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ファイル書き込み */ if ( 0 == ::WritePrivateProfileString( L "SECTION" , L "key_str" , L "テスト文字列" , strInfilePath.c_str() ) ) { std::wcout << L "iniファイルへの書き込みに失敗しました。" << std::endl; } else { std::wcout << L "iniファイルへ書き込みました。" << std::endl; } /* iniファイル書き込み(数値) 読み出しではも、GetProfileInt()が存在しますが、WritePrivateProfileIntは 存在しないので、数値は文字列化して書き込みます。 */ if ( 0 == ::WritePrivateProfileString( L "SECTION" , L "key_value" , L "1234" , strInfilePath.c_str() ) ) { std::wcout << L "iniファイルへの書き込みに失敗しました。" << std::endl; } else { std::wcout << L "iniファイルへ書き込みました。" << std::endl; } // 正常終了 return ( 0 ); } |
iniファイルへ書き込みました。 iniファイルへ書き込みました。