C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 31 を表示中)
■15215 / )  Re[17]: std::ofstream とCFileの書き込み速度
□投稿者/ セイン (80回)-(2008/03/07(Fri) 10:19:27)
No15210 (アキラ さん) に返信
> ■No15209 (セイン さん) に返信
> 
>>自分なりにofstreamの中を追いかけてみたところ、
>>以下の関数を使って書き込みをしていました。
>>C言語のfputsじゃなかったんですね^^;
>>basic_streambuf::sputn
>>basic_streambuf::sputc
>>
> 
> いえ、VC++8.0のofstreamはfwrite, fputcを使ってます

あうぅ。ごめんなさい僕の力不足です。そうなんですね。

以下ソースになるのですが、速度にそれぞれ差が出ました。
一番下に書いてあるのが、fputcになります。
ofstreamよりfputcのほうが僕の環境では遅くなります。



メイン関数側
	CLogHlp m_Log;
	m_Log.CreateWriteFile("C:\\tmp.txt");

	CString str;
	for(int i = 0; i < 100000; i++) {
		str.Format("%10d\t", i);
		m_Log.Write(str);
	}

	m_Log.WirteFileUnload();




ログ書き込みクラス
#pragma once
#include <fstream>

class CLogHlp
{
public:
	CLogHlp() 
	{
	}

	~CLogHlp() 
	{
		try {
			WirteFileUnload();
		} catch(...) {}
	}

	/** ファイル読み込み @return TRUE:オープン成功 FALSE:失敗 */
	BOOL CreateWriteFile
	(
		LPCSTR path					///< [I]ファイルパス
	)
	{
		int ret = TRUE;
		try{
			// ファイル作成
			if(mWriteFile.Open(path, CFile::modeCreate|CFile::modeWrite) != TRUE) {
				ret = FALSE;
			}
		}catch(...) {
			ret = FALSE;
		}
		return ret;
	}

	/* オープン中のファイルクローズ */
	void WirteFileUnload()
	{
		if(mWriteFile.m_hFile != CFile::hFileNull) {
			mWriteFile.Close();
		}
	}

	/* 書き込み */
	BOOL Write
	(
		CString str					///< [I]保存内容
	)
	{
		int ret = TRUE;
		try {
			mWriteFile.Write(str, str.GetLength());
		}
		catch(...) {
			ret = FALSE;
		}
		return ret;
	}

private:
	// 書き込み用ファイル
	CFile mWriteFile;
};



class CLogHlp
{
public:
	CLogHlp() 
	{
	}

	~CLogHlp() 
	{
		try {
			WirteFileUnload();
		} catch(...) {}
	}

	/** ファイル読み込み @return TRUE:オープン成功 FALSE:失敗 */
	BOOL CreateWriteFile
	(
		LPCSTR path					///< [I]ファイルパス
	)
	{
		try {
			setlocale(LC_ALL, "Japanese"); 
			// ファイルオープン
			fsm.open(path, std::ios::trunc | std::ios::out);
			if(fsm.is_open() == false)
			{
				// 既にオープン中
				return FALSE;
			}
		}
		catch(...) {
			return FALSE;
		}
		return TRUE;
	}

	/* オープン中のファイルクローズ */
	void WirteFileUnload()
	{
		if(fsm.is_open() == true)
		{				
			// ファイルのクローズ
			fsm.close();
		}
	}

	/* 書き込み */
	BOOL Write
	(
		CString str					///< [I]保存内容
	)
	{
		int ret = TRUE;
		try {
			fsm << (LPCSTR)str;
		}
		catch(...) {
			ret = FALSE;
		}
		return ret;
	}

private:
	// ファイルストリーム
	std::ofstream	fsm;
};



class CLogHlp
{
public:
	CLogHlp() 
	{
	}

	~CLogHlp() 
	{
		try {
			WirteFileUnload();
		} catch(...) {}
	}

	/** ファイル読み込み @return TRUE:オープン成功 FALSE:失敗 */
	BOOL CreateWriteFile
	(
		LPCSTR path					///< [I]ファイルパス
	)
	{
		try {
		  if((fopen_s( &fp, path, "w" )) != 0 ){
			  return FALSE;
		  }

		}
		catch(...) {
			return FALSE;
		}
		return TRUE;
	}

	/* オープン中のファイルクローズ */
	void WirteFileUnload()
	{
		try {
			fclose(fp);
		}
		catch(...) {
		}
	}

	/* 書き込み */
	BOOL Write
	(
		CString str					///< [I]保存内容
	)
	{
		int ret = TRUE;
		try {
			fputs(str, fp);
		}
		catch(...) {
			ret = FALSE;
		}
		return ret;
	}

private:
	// ファイルストリーム
	FILE *fp;
};

返信 編集キー/


管理者用

- Child Tree -