テキストファイル

  使用時に実装



  
// *********************************************************
// テキストファイルの処理
// *********************************************************
void MyClass::LoadTextFile()
{
	LboxTextFile Txt;
}
  

※ 一つのオブジェクトでオープンできるファイルは一度に一つだけです
※ ツールオブジェクトのようにグローバルに定義して共有するほうが便利な場合もあります




  リストビューへ読み込み



テキストファイルを1行づつ読み込みます
※ テキスト行の最後尾には \n が付加されているので削除しています

  
// *********************************************************
// テキストファイルの処理
// *********************************************************
void MyClass::LoadTextFile()
{
	LboxTextFile Txt;

	// 「ファイルを開く」ダイアログ用
	LboxString LString;
	LboxCommdlg Commdlg( this->hWnd );

	Commdlg.lpstrTitle = "ファイルを開く";
	Commdlg.lpstrFilter = "テキスト,*.txt,全て,*.*";

	// コモンダイアログ呼び出し
	if ( !Commdlg.OpenFileName( &LString ) ) {
		MsgOk( "キャンセルされました   " );
		return;
	}

	// ファイルのオープン
	if ( !Txt.ReadOpen( &LString ) ) {
		MsgOk(
			"%s をオープンできません   ",
			LString.szLboxString
		);
		return;
	}

	// 読み込みバッファ
	LboxString LBuffer;

	// 一行の読み込みに十分なサイズを確保
	LBuffer.Resize( 1024 );

	// データ表示用のリストビューを初期化
	LView->Initialize();
	LView->AddColumn( "行番号" );
	LView->AddColumn( "テキストファイルの行データ" );

	int nRow;	// 行番号
	nRow = 1;

	LboxString LWork;	// 最後尾文字列処理用

	// 読み込み
	while( Txt.Get( &LBuffer ) ) {
		LView->AddRow();
		LView->SetColumnPrintf( 0, "%d", nRow );
		nRow++;
		LBuffer.Right( 1, &LWork );
		if ( LWork.operator == ("\n") ) {
			LBuffer.RemoveLastchar();
		}
		LView->SetColumnText( 1, &LBuffer );
	}

	// ファイルのクローズ
	Txt.Close();

	// リストビュー表示の最適化
	LView->Fit();
}
  

リストビュー内ではタブは正しく表示されませんので、
簡易的にインデント等を見やすくするには以下のようにします


  
	// 読み込み
	while( Txt.Get( &LBuffer ) ) {
		LView->AddRow();
		LView->SetColumnPrintf( 0, "%d", nRow );
		nRow++;
		LBuffer.Replace( "\t", "    " );  // タブをスペース4バイトに変換
		LBuffer.Right( 1, &LWork );
		if ( LWork.operator == ("\n") ) {
			LBuffer.RemoveLastchar();
		}
		LView->SetColumnText( 1, &LBuffer );
	}
  



  リストビューのデータを全て書き込み

テキストファイルへ1行づつ書き込みます

  
// *********************************************************
// テキストファイルの処理
// *********************************************************
void MyClass::SaveTextFile()
{
	int nCols;	// カラム数
	nCols = LView->ColumnCount();
	if ( nCols == 0 ) {
		MsgOk( "カラムがありません   " );
		return;
	}
	if ( LView->Count() == 0 ) {
		MsgOk( "行データがありません   " );
		return;
	}

	LboxTextFile Txt;

	// 「ファイルを保存する」ダイアログ用
	LboxString LString;
	LboxCommdlg Commdlg( this->hWnd );

	Commdlg.lpstrTitle = "ファイルを保存する";
	Commdlg.lpstrFilter = "テキスト,*.txt,全て,*.*";
	Commdlg.lpstrDefExt = "txt";

	// コモンダイアログ呼び出し
	if ( !Commdlg.SaveFileName( &LString ) ) {
		MsgOk( "キャンセルされました   " );
		return;
	}

	// ファイルのオープン
	if ( !Txt.WriteOpen( &LString ) ) {
		MsgOk(
			"%s をオープンできません   ",
			LString.szLboxString
		);
		return;
	}

	// 書き込みバッファ
	LboxString LBuffer;
	// カラム用バッファ
	LboxString LColumn;
	// ループ用ワーク
	int i,nRow;
	nRow = -1;

	// リストビューからデータ取得
	while( LView->FindNextRow( &nRow ) ) {
		LString.SetChar( 0, 0 );  // 文字列の先頭に 0x00 をセット
		for( i = 0; i < nCols; i++ ) {
			if ( i != 0 ) {
				LString.operator += (",");
			}
			LView->GetColumnText( i, &LColumn );
			LString.operator += ( &LColumn );
		}
		LString.operator += ( "\n" );	// 改行
		Txt.Put( &LString );
	}

	// ファイルのクローズ
	Txt.Close();
}
  



  テキストエリアの内容を全て書き込み

テキストファイルへメモリから一括で書き込みます

  
// *********************************************************
// テキストファイルの処理
// *********************************************************
void MyClass::SaveTextArea()
{
	LboxTextFile Txt;

	// 「ファイルを保存する」ダイアログ用
	LboxString LString;
	LboxCommdlg Commdlg( this->hWnd );

	Commdlg.lpstrTitle = "ファイルを保存する";
	Commdlg.lpstrFilter = "テキスト,*.txt,全て,*.*";
	Commdlg.lpstrDefExt = "txt";

	// コモンダイアログ呼び出し
	if ( !Commdlg.SaveFileName( &LString ) ) {
		MsgOk( "キャンセルされました   " );
		return;
	}

	// ファイルのオープン
	if ( !Txt.WriteOpen( &LString ) ) {
		MsgOk(
			"%s をオープンできません   ",
			LString.szLboxString
		);
		return;
	}

	// 書き込みバッファ
	LboxString LBuffer;

	LEdit->GetText( &LString );
	Txt.Put( &LString );

	// ファイルのクローズ
	Txt.Close();
}
  



  テキストエリアへ読み込み

テキストファイルからメモリへ一括で読み込みます
※ テキストデータかどうか開く前にチェックしています

  
// *********************************************************
// テキストファイルの処理
// *********************************************************
void MyClass::LoadTextArea()
{
	LboxTextFile Txt;

	// 「ファイルを開く」ダイアログ用
	LboxString LString;
	LboxCommdlg Commdlg( this->hWnd );

	Commdlg.lpstrTitle = "ファイルを開く";
	Commdlg.lpstrFilter = "テキスト,*.txt,全て,*.*";

	// コモンダイアログ呼び出し
	if ( !Commdlg.OpenFileName( &LString ) ) {
		MsgOk( "キャンセルされました   " );
		return;
	}

	// テキストファイルかどうかのチェック
	LboxFileSystem Fs;
	if ( !Fs.IsText( &LString ) ) {
		MsgOk( "テキストファイルではありません   " );
		return;
	}

	// ファイルのオープン
	if ( !Txt.ReadOpen( &LString ) ) {
		MsgOk(
			"%s をオープンできません   ",
			LString.szLboxString
		);
		return;
	}

	// 読み込みバッファ
	LboxString LBuffer;

	// 読み込み
	Txt.GetAll( &LBuffer );
	// データ内には \n しか含まれていないのでテキストエリア用に変換
	LBuffer.Replace( "\n", "\r\n" );
	LEdit->SetText( &LBuffer );

	// ファイルのクローズ
	Txt.Close();
}
  



  ドラッグドロップで読み込む

ProcDrop はオーバロード用関数です

※ WMCreate で LEdit->DragAccept( true ); を実行しておいて下さい

  
// *********************************************************
// ドラッグドロップで読み込む
// *********************************************************
void MyClass::ProcDrop()
{
	if ( !LEdit->IsHandle( this->hTargetWindow ) ) {
		return;
	}
	
	LboxTextFile Txt;
	LboxString LString;

	// ドロップされたファイルのパスの取得
	LString.Resize( MAX_PATH );
	DragQueryFile(
		this->hDrop,
		0,  // 選択された先頭ファイル
		LString.szLboxString,
		MAX_PATH
	);

	// テキストフアイルかどうかのチェック
	LboxFileSystem Fs;
	if ( !Fs.IsText( &LString ) ) {
		MsgOk( "テキストファイルではありません   " );
		return;
	}

	// ファイルのオープン
	if ( !Txt.ReadOpen( &LString ) ) {
		MsgOk(
			"%s をオープンできません   ",
			LString.szLboxString
		);
		return;
	}

	// 読み込みバッファ
	LboxString LBuffer;

	// 読み込み
	Txt.GetAll( &LBuffer );
	// データ内には \n しか含まれていないのでテキストエリア用に変換
	LBuffer.Replace( "\n", "\r\n" );
	LEdit->SetText( &LBuffer );

	// ファイルのクローズ
	Txt.Close();

}
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ