掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
文字化けを防止するためには?? (ID:46252)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
>RichEditのLines.LoadFromFileで読み込むと、確かに >“リンゴ”の一部がおかしくなりますね・・・ igy さん.テストしてみました. TStringList に一揆に読み込んでから,TRichEdit.Text に代入する方法です. こちらの方が高速です. 確認したところ,文字化けはないようです. 手順は以下の通りです. 動作確認は,Windows 7 U64(SP1) + Delphi XE, XE2 Pro です. (1) 新規プロジェクトを作成 (2) フォームに TRichEdit を 1 つ.TButton を 3 つ配置 (3) ボタンクリックのイベント等を作成 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ComCtrls; type TForm1 = class(TForm) Button1: TButton; RichEdit1: TRichEdit; Button2: TButton; Button3: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private 宣言 } SaveFilePath : String; TestText : String; LinesCnt : Integer; public { Public 宣言 } end; var Form1: TForm1; implementation {$R *.dfm} //============================================================================= // フォーム生成時の処理 //============================================================================= procedure TForm1.FormCreate(Sender: TObject); begin //Clearしてフォントを指定しないと設計時の書式が適用される RichEdit1.Lines.Clear; RichEdit1.Font.Name := 'MS ゴシック'; //プレーンテキストのみ扱う RichEdit1.PlainText := True; //スクロールバーを表示 RichEdit1.ScrollBars := ssBoth; //ファイル名.念のため絶対パスにしておく SaveFilePath := ExpandFileName('000.txt'); //テスト用文字列 TestText := 'A,0001,リンゴ,1000,20000'; //作成する行数 LinesCnt := 100000; end; //============================================================================= // テスト用テキストファイルの作成 //============================================================================= procedure TForm1.Button1Click(Sender: TObject); var SWriter : TStreamWriter; i : Integer; begin //保存するファイル //そのまま保存するとUTF-8Nになるので,エンコードしてShift-JISで保存 SWriter := TStreamWriter.Create(SaveFilePath, False, TEncoding.GetEncoding(932)); try for i := 0 to LinesCnt - 1 do begin SWriter.WriteLine(TestText); Application.ProcessMessages; end; finally SWriter.Close; SWriter.Free; end; Application.MessageBox('作成しました', '情報'); end; //============================================================================= // 保存したテキストファイルを読み出して表示 //============================================================================= procedure TForm1.Button2Click(Sender: TObject); var SL : TStringList; begin RichEdit1.Lines.Clear; SL := TStringList.Create; try SL.LoadFromFile(SaveFilePath); RichEdit1.Text := SL.Text; Application.MessageBox('読み込みました', '情報'); finally FreeAndNil(SL); end; end; //============================================================================= // RichEditのテキストを保存 //============================================================================= procedure TForm1.Button3Click(Sender: TObject); begin if RichEdit1.Lines.Count > 0 then begin //Shift-JISで保存される RichEdit1.Lines.SaveToFile(SaveFilePath); Application.MessageBox('保存しました', '情報'); end else begin Application.MessageBox('テキストがありせん', '情報'); end; end; end. 元の質問をされた方へ. こういうのを「テスト可能なコード」と言います.
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.