ページ 1 / 1
iniファイル保存場所の変更
Posted: 2009年12月02日(水) 14:54
by ななし
visualStudio2005、C++を使用しています。
実行ファイルと同じ場所にiniファイルを作成しようとしているのですが、
検索するとMFC利用を前提とした解説ばかりです。
win32apiを利用したやり方というのはあるのでしょうか?
わからない所というのが、
MFCの解説だとCWinAppのクラスメンバであるm_pszProfileNameに
実行ファイルへのパスを上書きする、とあるのですが
win32apiですとこの変数に当るものがあるのでしょうか?(get××()みたいな関数でゲットできたり)
よろしくお願い致します。
Re:iniファイル保存場所の変更
Posted: 2009年12月02日(水) 15:11
by Mist
Re:iniファイル保存場所の変更
Posted: 2009年12月02日(水) 15:25
by バグ
アプリ起動直後にカレントディレクトリパスを保持しておくのがいいんじゃないかな?
GetCurrentDirectoryというそのまんまな名前の関数がありますんで、調べてみてください。
Re:iniファイル保存場所の変更
Posted: 2009年12月02日(水) 16:21
by softya
アプリの配布を考えていて、Program Filesの下にインストールしてもらうつもりなら、Vista以降OSでは思っても見ない場所にiniファイルが作られる事になりますので注意が必要です。
前にブログで記事を書いていたので参考にしてください。
http://softyasu.blog121.fc2.com/blog-entry-68.html
Re:iniファイル保存場所の変更
Posted: 2009年12月02日(水) 16:24
by ななし
>Mistさん
>バグさん
上記のアドバイスを参考にしてみた結果、
GetPrivateProfileString();でiniファイルへのパスを設定できそうです。
ありがとうございました。
Re:iniファイル保存場所の変更
Posted: 2009年12月02日(水) 18:15
by バグ
昔作ったINIファイルへの読み書きを多少楽に扱えるクラスを紹介しておきます。
INIファイルは便利な反面、文字列への変換作業が面倒なんですよね。
その面倒さを軽減させる為だけに作ったのが以下のクラスです(笑)
CConvertは文字列と値の相互変換を行うクラスで、CIniFileはCConvertを利用して書き込みと読み込みを多少楽に行うクラスです。
VisualStudio2005で作成しております。
よかったらどうぞ♪
#pragma once
#include <string>
#include <sstream>
#include <windows.h>
template <typename Type>
class CConvert
{
public:
CConvert(void)
{
}
virtual ~CConvert(void)
{
}
// Type型の値から文字列への変換
// Type tValue = 変換したい値
// return = 変換された文字列
static std::string ToString(Type tValue)
{
std::stringstream cStringStream;
cStringStream.setf(std::ios::fixed, std::ios::floatfield);
cStringStream << tValue;
return cStringStream.str();
}
// 文字列からType型の値への変換
// std::string szString = 変換したい文字列
// return = 変換されたType型の値
static Type ToValue(std::string szString)
{
std::stringstream cStringStream;
cStringStream << szString;
Type tValue;
cStringStream >> tValue;
return tValue;
}
};
template <typename Type>
class CIniFile
{
public:
CIniFile(void)
{
}
virtual ~CIniFile(void)
{
}
// INIファイルへの書き込み
// const char* pszFilePath = 書き込み先ファイルのパス
// const char* pszSection = セクション
// const char* pszKey = キー
// Type tValue = 書き込むデータ
static void Write(const char* pszFilePath, const char* pszSection, const char* pszKey, Type tValue)
{
WritePrivateProfileStringA(pszSection, pszKey, CConvert<Type>::ToString(tValue).c_str(), pszFilePath);
}
// INIファイルからの読み込み
// const char* pszFilePath = 書き込み先ファイルのパス
// const char* pszSection = セクション
// const char* pszKey = キー
// Type tDefault = 読み込みに失敗した際に代入される値
// int nDataLength = 読み込みに使用するバッファ長
// return = ファイルから読み込まれたType型の値
static Type Read(const char* pszFilePath, const char* pszSection, const char* pszKey, Type tDefault, int nDataLength)
{
char* szBuf = new char[nDataLength];
GetPrivateProfileStringA(pszSection, pszKey, CConvert<Type>::ToString(tDefault).c_str(), szBuf, nDataLength, pszFilePath);
Type tValue = CConvert<Type>::ToValue(szBuf);
delete[/url] szBuf;
return tValue;
}
};