๐Ÿ’พ Archived View for bbs.geminispace.org โ€บ s โ€บ pascal โ€บ 17038 captured on 2024-12-17 at 12:08:38. Gemini links have been rewritten to link to archived content

View Raw

More Information

โฌ…๏ธ Previous capture (2024-09-29)

๐Ÿšง View Differences

-=-=-=-=-=-=-

Pascal and files.

I am looking for a guide to use text files in freepascal. I am looking to make a gemtext to html tool because it sounds like a good project.

The link conversation is going to be a challenge but I am looking forward to it.

Posted in: s/pascal

๐Ÿš‚ MrSVCD

May 17 ยท 7 months ago ยท ๐Ÿ‘ norayr

10 Comments โ†“

๐Ÿ™ norayr [mod] ยท 2024-05-17 at 11:35:

the old turbo pascal compatible way to do it is this:

$ cat test0.pas

var
  f: textfile;
  s: string;
begin
  assign(f, '/etc/passwd');
  reset(f);
  while not eof(f) do
  begin
    readln(f, s);
    writeln(s);
  end;
end.

instead of reset() you can use append() - that would mean that you can write to the end of the file. there is also rewrite() - that means you will erase whatever there is in the file and write to it from the beginning.

these file handling routines are in unit system, which is included in pascal by default, even if you don't add it to uses section.

๐Ÿ™ norayr [mod] ยท 2024-05-17 at 11:37:

some links:

โ€” https://wiki.freepascal.org/Assign

โ€” https://wiki.freepascal.org/Rewrite

โ€” https://wiki.freepascal.org/Category:File_Handling

โ€” https://wiki.freepascal.org/File_Handling_In_Pascal

๐Ÿ™ norayr [mod] ยท 2024-05-17 at 11:39:

new, oop way of doing things is via streams:

program InOutStream;

{$mode objfpc}

uses
  Classes, SysUtils;
var
  InStream, OutStream: TFileStream;
begin
  if (ParamCount = 2) and
    FileExists(ParamStr(1)) and
    FileExists(ParamStr(2)) then
  begin
    InStream := TFileStream.Create(ParamStr(1), fmOpenRead);
    try
      OutStream := TFileStream.Create(Paramstr(2), fmOpenWrite);
      try
        OutStream.Position := OutStream.size;
        OutStream.CopyFrom(InStream, 0); // appends
      finally
        OutStream.Free;
      end;
    finally
      InStream.Free;
    end;
  end else 
    Writeln('Usage: inoutstream <infile> <outfile>');
end.

๐Ÿ™ norayr [mod] ยท 2024-05-17 at 11:41:

in first old style example you can use read() to read only one character, to create a parser.

๐Ÿ™ norayr [mod] ยท 2024-05-17 at 11:42:

โ€” i found a markdown parser for freepascal.

๐Ÿ™ norayr [mod] ยท 2024-05-17 at 11:43:

wow, it also generates html from markdown!

๐Ÿ™ norayr [mod] ยท 2024-05-17 at 11:45:

in the demos i see .lpi file, that's a file for a lazarus ide. you can work without the ide, you would need to change the demos to not require graphics at all, and instead of showing graphically, to use input and output files.

๐Ÿš‚ MrSVCD [OP] ยท 2024-05-17 at 13:23:

Thank you.

If I can make it compatible with Hispeed Pascal for amiga would be a bonus. Hispeed say that they are almost fully TP 5 compatible.

๐Ÿ™ norayr [mod] ยท 2024-05-17 at 14:37:

then the first way should work. yes, it would be very cool to have the same source for both amiga and pc. btw, freepascal also can compile for amiga.

โ€” channel

contains lots of freepascal amiga demos.

๐Ÿ™ norayr [mod] ยท Aug 08 at 00:28:

i just noticed that the litk to the channel is broken.

โ€” this is the link