Recent

Author Topic: TStrings.LoadFromFileLoad  (Read 1254 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
TStrings.LoadFromFileLoad
« on: September 21, 2022, 07:38:27 am »
I have the following code and getting an error at line 8. (Access Violation.)
Basically I'm trying to load a text file into a TRichMemo. Then to a TStream and then to a TRichMemo.

Is this how it's done or is there an easier way?

Thanks

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.  Var TS: TStrings;
  3.  begin
  4.   RM1.Clear;
  5.   TS.LoadFromFile( 'My.txt'  );
  6.  end;
  7. end.  
« Last Edit: September 21, 2022, 07:41:44 am by JLWest »
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: TStrings.LoadFromFileLoad
« Reply #1 on: September 21, 2022, 07:54:12 am »
TS isn't initialized.

But look at your other topic for a more complete answer.
https://forum.lazarus.freepascal.org/index.php/topic,60656.msg454469.html#msg454469

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TStrings.LoadFromFileLoad
« Reply #2 on: September 21, 2022, 08:41:04 am »
I don't understand TS isn't initialized.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: TStrings.LoadFromFileLoad
« Reply #3 on: September 21, 2022, 08:54:42 am »
I don't understand TS isn't initialized.

Code: Pascal  [Select][+][-]
  1.     procedure TForm1.Button1Click(Sender: TObject);
  2.      Var TS: TStrings;
  3.      begin
  4.       RM1.Clear;
  5.       TS := TStrings.Create; // add this
  6.       TS.LoadFromFile( 'My.txt'  );
  7.      end;
  8.     end.

Later you have to do TS.Free; somewhere.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TStrings.LoadFromFileLoad
« Reply #4 on: September 21, 2022, 09:03:47 am »
I get an error on line 5 >>> unit1.pas(17,3) Error: An interface, helper or Objective-C protocol or category cannot contain fields

Have no idea; tried
 TRichMemoHelper = class helper for TRichMemo;
 TRichMemoHelper : class helper for TRichMemo;      unit1.pas(15,47) Fatal: Syntax error, "identifier" expected but ";" found

Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   { TForm1 }
  4.  
  5.   TRichMemoHelper = class helper for TRichMemo
  6.  
  7.   TForm1 = class(TForm)
  8.     Button1: TButton;
  9.     RM1: TRichMemo;
  10.     procedure Button1Click(Sender: TObject);
  11.  
  12.   private
  13.  
  14.   public
  15.   procedure LoadRTFFromFile(const FileName: string);
  16.  
  17.   end;
  18.  
  19. var
  20.   Form1: TForm1;
  21.  
  22. implementation
  23.  
  24. {$R *.lfm}
  25.  
  26. { TForm1 }
  27.  
  28. procedure LoadRTFFromFile(const FileName: string);
  29.  Var Stream: TStream;
  30.  begin
  31.   Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
  32.    try
  33.     Self.LoadRichText(Stream);
  34.    finally
  35.     Stream.Free;
  36.  end;
  37.  end;
  38.  
  39. procedure TForm1.Button1Click(Sender: TObject);
  40.  begin
  41.    RichMemo1.LoadRTFFromFile('c:\temp\test.rtf');
  42. end;                                  
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: TStrings.LoadFromFileLoad
« Reply #5 on: September 21, 2022, 09:06:42 am »
I get an error on line 5 >>> unit1.pas(17,3) Error: An interface, helper or Objective-C protocol or category cannot contain fields
Wow, we're bouncing all over the forum.
Please keep to one topic.

This code wasn't a complete unit.
You would need to put this in your own form-unit or create a proper helper-unit.
(With unit at the top etc.)

O, and you're missing a ; at the end of line 5.
But you could also put all this in the implementation part.
The class defenition doesn't need to be at the top.

« Last Edit: September 21, 2022, 09:08:26 am by rvk »

bytebites

  • Hero Member
  • *****
  • Posts: 633
Re: TStrings.LoadFromFileLoad
« Reply #6 on: September 21, 2022, 09:14:06 am »
Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   { TForm1 }
  4.  
  5.  
  6.   TForm1 = class(TForm)
  7.     Button1: TButton;
  8.     RM1: TRichMemo;
  9.     procedure Button1Click(Sender: TObject);
  10.  
  11.   end;
  12.  
  13.   TRichMemoHelper = class helper for TRichMemo
  14.   public
  15.     procedure LoadRTFFromFile(const FileName: string);
  16.  
  17.   end;
  18.  
  19. var
  20.   Form1: TForm1;
  21.  
  22. implementation
  23.  
  24. {$R *.lfm}
  25.  
  26. { TForm1 }
  27.  
  28. procedure TRichMemoHelper.LoadRTFFromFile(const FileName: string);
  29.  Var Stream: TStream;
  30.  begin
  31.   Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
  32.    try
  33.     Self.LoadRichText(Stream);
  34.    finally
  35.     Stream.Free;
  36.  end;
  37.  end;
  38.  
  39. procedure TForm1.Button1Click(Sender: TObject);
  40.  begin
  41.    RichMemo1.LoadRTFFromFile('c:\temp\test.rtf');
  42. end;                                  

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TStrings.LoadFromFileLoad
« Reply #7 on: September 21, 2022, 10:15:26 am »
sorry for bouncing around like that. It's late here and I really don';t understand this very well. Can't seem to get it to compile.

Thanks for the help, Here is the error I'm getting but no line number. it highlights Line 14.

Compile Project, Target: RichText.exe: Exit code 1, Errors: 1
unit1.pas(15,47) Fatal: Syntax error, "identifier" expected but ";" found




Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6. uses
  7.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, StdCtrls,
  8.   RichMemo;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TRichMemoHelper = class helper for TRichMemo;
  15.  
  16.   TForm1 = class(TForm)
  17.     Button1: TButton;
  18.     RM1: TRichMemo;
  19.     procedure Button1Click(Sender: TObject);
  20.  
  21.   private
  22.   public
  23.   procedure LoadRTFFromFile(const FileName: string);
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure LoadRTFFromFile(const FileName: string);
  36.  Var Stream: TStream;
  37.  begin
  38.   Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
  39.    try
  40.     Self.LoadRichText(Stream);
  41.    finally
  42.     Stream.Free;
  43.  end;
  44.  end;
  45.  
  46. procedure TForm1.Button1Click(Sender: TObject);
  47.  begin
  48.    RichMemo1.LoadRTFFromFile(My.rtf');
  49. end;
  50. end.
  51.  
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: TStrings.LoadFromFileLoad
« Reply #8 on: September 21, 2022, 10:21:20 am »
Thanks for the help, Here is the error I'm getting but no line number. it highlights Line 14.
Compile Project, Target: RichText.exe: Exit code 1, Errors: 1
unit1.pas(15,47) Fatal: Syntax error, "identifier" expected but ";" found
bytebites already showed above how you could do it.

You need to put the class helper line from line 14 into the implementation.
Like this:
Code: Pascal  [Select][+][-]
  1. type
  2.  TRichMemoHelper = class helper for TRichMemo
  3.   public
  4.     procedure LoadRTFFromFile(const FileName: string);
  5.   end;
You can put this just above the procedure LoadRTFFromFile (line 35).

Then remove the procedure LoadRTFFromFile from your TForm1 class (line 23).

Then it should work.

Alternatively you could also forget about using a class helper and put RichMemoUtils in your uses clause and use this
Code: Pascal  [Select][+][-]
  1. LoadRTFFromFile(RichMemo1, 'my_rtf_file.rtf');
(in that case you can't use the notation of RichMemo1.LoadRTFFrom but need to put the RichMemo1 in as parameter to that function)

Either way will work.
« Last Edit: September 21, 2022, 10:23:23 am by rvk »

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TStrings.LoadFromFileLoad
« Reply #9 on: September 21, 2022, 11:03:00 am »
Thanks all, It's late and I make it work. Hac=ve to give up. Maybe later.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: TStrings.LoadFromFileLoad
« Reply #10 on: September 22, 2022, 12:13:12 am »
Code: Pascal  [Select][+][-]
  1.   RM1.lines.LoadFromFile( 'My.txt'  );

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: TStrings.LoadFromFileLoad
« Reply #11 on: September 22, 2022, 03:32:44 pm »
I am surprised that no one commented on the fact that you can't directly use a Tstrings  like that.
The only true wisdom is knowing you know nothing

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TStrings.LoadFromFileLoad
« Reply #12 on: September 22, 2022, 05:47:30 pm »
For what ever reason I can't make this work. I originally wanted to have a TRichMemo on a form and load it with a text file from disk. It was just to see how it worked. I keep getting errors and don'tt understand any of them. So I think I'll give up on the idea or try at a later date.

Thanks all
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: TStrings.LoadFromFileLoad
« Reply #13 on: September 22, 2022, 06:21:37 pm »
RM1.LoadRichText(........)?
The only true wisdom is knowing you know nothing

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: TStrings.LoadFromFileLoad
« Reply #14 on: September 26, 2022, 07:00:44 pm »
For what ever reason I can't make this work. I originally wanted to have a TRichMemo on a form and load it with a text file from disk. It was just to see how it worked. I keep getting errors and don'tt understand any of them. So I think I'll give up on the idea or try at a later date.

Thanks all

This feels very familiar to me. Some weeks ago I said to myself: What a waste of live-time for some bytes!
I found the way out, - after some days of frustration.

The reason, why it did not work in my case, was the internal format.
A file can contain rtf-format or txt-format.
And it can be saved by the 2 methods of this thread in both ways and with both endings: *rtf and *txt
Never the less it cannot be re-loaded if criss-crossed saved.

 

TinyPortal © 2005-2018