So, I was writing this little bit of code when I noticed... Well, I really don't know what the heck I noticed, so we'll just say I noticed "something kind of funny". When you save a
TMemo to file, it saves it exactly as it shows on the screen. Take, for example, the snippet below (the complete code is at the bottom).
For i := 0 to Transcript.Count - 1 Do SinStr := SinStr + ' ' + Transcript[i]; // Make Transcript into one large string.
Memo1.Text := SinStr;
Finally
Input.Free;
Transcript.Free;
End;
End;
Procedure TForm1.SaveClick(Sender: TObject);
Var
FileName : String = 'Transcript1.txt';
Begin
Memo1.Lines.SaveToFile(FileName);
ShowMessage('Transcript saved successfully to: ' + FileName);
End;
- I load a bunch of strings into a TStringList.
- I take the TStringList and concatenate all the strings into one real long string.
- I put the one big string into a TMemo.
- Lastly, I save the contents of the TMemo in a file.
So when I loaded the file
'Transcript1.txt' to Grammarly, I noticed it was back to being a bunch of little strings! Grammarly didn't like that. Now don't ask me how, but I found out that depending on the width of the
TMemo, that determines the length of the strings in the file!!
Whaaaat??Now, I realize what's going on with the
Memo1.Lines.SaveToFile(FileName); (Thank God, I at least know that).
I'm guessing that I can save the
SinStr instead of the
Memo1.Lines and (I didn't really try this, so I'm guessing) it'll be one string in the file. The only way I know how to do that is by declaring
SinStr as a global variable (because the concatenating and the saving are in two different procedures). But, from all the examples I've seen, global variables are a "no, no" (I think I may have even read it somewhere too).
I tried
Memo1.Text.SaveToFile(FileName);, but that just gave me an "Illegal qualifier" error.
So, how can I save the contents of a TMemo as one string in a file?--or--Is there a way to save a variable in the class declaration?Unit Transkripts;
{$mode objfpc}{$H+} // Converts a file or pasted text into just the odd lines or just the even lines.
Interface
Uses Classes, Dialogs, Forms, Graphics, StdCtrls, ExtCtrls, Buttons; // SysUtils, Controls,
Type
{ TForm1 } // Use a TMemo to paste in the text to be converted.
TForm1 = class(TForm)
Save: TBitBtn;
Convert: TButton; // Starts the conversion.
Label1: TLabel; // Just shows the title of this application.
Memo1: TMemo; // Shows the contents of the file. Maybe figure a way to switch from input file to output file.
Panel1: TPanel; // Green if loaded and saved successfully.
OddEven: TToggleBox; // Toggle between keeping odd or even Lines.
procedure ConvertClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure OddEvenChange(Sender: TObject);
procedure OddEvenClick(Sender: TObject);
procedure SaveClick(Sender: TObject);
Private
Procedure Load;
Public
End;
{ TODO -o'Del Guy' -cCoding : Add mechanism for detecting text in another Memo and deciding to convert the Memo text
or load file if Memo is empty }
Var
Form1: TForm1;
Implementation
{$R *.lfm}
{ TForm1 }
Procedure TForm1.FormCreate(Sender: TObject);
Begin
End;
Procedure TForm1.OddEvenChange(Sender: TObject);
Begin
End;
Procedure TForm1.FormResize(Sender: TObject);
Begin
Memo1.Height := Form1.Height - 64
End;
Procedure TForm1.ConvertClick(Sender: TObject);
Begin
Load;
End;
Procedure TForm1.OddEvenClick(Sender: TObject);
Begin
If OddEven.Checked Then OddEven.Caption := 'Odd' Else OddEven.Caption := 'Even';
If OddEven.Checked Then Panel1.Color := clRed Else Panel1.Color := clLime
End;
Procedure TForm1.Load;
Var
Input : TStringList; // Holds the transcript before converting.
Transcript : TStringList; // Holds the actual transcript after conversion
SinStr : String; // Holds the transcript as a single string.
i : Integer; // Iterator Variable
Begin
SinStr := '';
Input := TStringList.Create;
Transcript := TStringList.Create;
Input.LoadFromFile('Transcript.txt');
Try
For i := 1 to Input.Count - 1 Do
Begin
If not OddEven.Checked AND (i mod 2 = 1) Then Transcript.Add(Input[i]); // Load only the odd or only the even lines.
End;
For i := 0 to Transcript.Count - 1 Do SinStr := SinStr + ' ' + Transcript[i]; // Make Transcript into one large string.
Memo1.Text := SinStr;
Finally
Input.Free;
Transcript.Free;
End;
End;
Procedure TForm1.SaveClick(Sender: TObject);
Var
FileName : String = 'Transcript1.txt';
Begin
Memo1.Lines.SaveToFile(FileName);
ShowMessage('Transcript saved successfully to: ' + FileName);
End;
end.