iniファイル保存場所の変更

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
ななし

iniファイル保存場所の変更

#1

投稿記事 by ななし » 15年前

visualStudio2005、C++を使用しています。
実行ファイルと同じ場所にiniファイルを作成しようとしているのですが、
検索するとMFC利用を前提とした解説ばかりです。
win32apiを利用したやり方というのはあるのでしょうか?

わからない所というのが、
MFCの解説だとCWinAppのクラスメンバであるm_pszProfileNameに
実行ファイルへのパスを上書きする、とあるのですが
win32apiですとこの変数に当るものがあるのでしょうか?(get××()みたいな関数でゲットできたり)
よろしくお願い致します。


バグ

Re:iniファイル保存場所の変更

#3

投稿記事 by バグ » 15年前

アプリ起動直後にカレントディレクトリパスを保持しておくのがいいんじゃないかな?
GetCurrentDirectoryというそのまんまな名前の関数がありますんで、調べてみてください。

softya

Re:iniファイル保存場所の変更

#4

投稿記事 by softya » 15年前

アプリの配布を考えていて、Program Filesの下にインストールしてもらうつもりなら、Vista以降OSでは思っても見ない場所にiniファイルが作られる事になりますので注意が必要です。

前にブログで記事を書いていたので参考にしてください。
http://softyasu.blog121.fc2.com/blog-entry-68.html

ななし

Re:iniファイル保存場所の変更

#5

投稿記事 by ななし » 15年前

>Mistさん
>バグさん
上記のアドバイスを参考にしてみた結果、
GetPrivateProfileString();でiniファイルへのパスを設定できそうです。
ありがとうございました。

バグ

Re:iniファイル保存場所の変更

#6

投稿記事 by バグ » 15年前

昔作った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;
	}
};

閉鎖

“C言語何でも質問掲示板” へ戻る