Recent

Author Topic: text (textfiles) type  (Read 1338 times)

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: text (textfiles) type
« Reply #15 on: March 18, 2022, 08:49:51 pm »

SEEK(0...) would probably overwrite the first few characters anyway (as in c)
You can easily slog out a prepend and append routine.

Code: Pascal  [Select][+][-]
  1.  {$IOCHECKS OFF}
  2. uses
  3. sysutils;
  4.  
  5. function filelength(filename:ansistring):longword;
  6. Var F : File of byte;
  7. var L:longword;
  8. begin
  9. Assign (F,filename);
  10.   Reset (F);
  11.   L:=FileSize(F);
  12.   Close (F);
  13.   exit(L);
  14. end;
  15.  
  16. procedure loadfile(var content: ansistring;filename:ansistring);
  17.    Var Fin : File;
  18.    Var x:longint;
  19.    begin
  20.    x:=filelength(filename);
  21.    setlength(content,x);
  22.    Assign (Fin,filename);
  23.    {$I-}
  24.    Reset (Fin,x);
  25.    {$I+}
  26.    if IOResult<>0 then
  27.   begin
  28.   Writeln ('Error opening file: ', filename);
  29.   exit
  30.   end;
  31.    BlockRead (Fin,content[1],1);
  32.    close(fin);
  33. end;
  34.  
  35.  procedure savefile(s:ansistring ;filename:ansistring);
  36.     var
  37.     fout:file;
  38.     begin
  39.     Assign(fout,filename);
  40.     Rewrite(fout,length(s));
  41.     blockwrite(fout,s[1],1);
  42.     close(fout);
  43.   end;
  44.  
  45.   procedure prepend(content:ansistring;filename:ansistring);
  46.   var
  47.   s,L:ansistring;
  48.   begin
  49.   loadfile(L,filename);
  50.   L:=content+L;
  51.   savefile(L,filename);
  52.   end;
  53.  
  54.  procedure append(content:ansistring;filename:ansistring);
  55.   var
  56.   s,L:ansistring;
  57.   begin
  58.   loadfile(L,filename);
  59.   L:=L+content;
  60.   savefile(L,filename);
  61.   end;
  62.  
  63. var
  64. s:ansistring='abcdefghijklmnopqrstuvwxyz';
  65. ans:ansistring;
  66. fname:ansistring='seektest.txt';
  67.  
  68.  begin
  69.  savefile(s,fname);
  70.  loadfile(ans,'seektest.txt');
  71.  writeln(ans);
  72.  prepend('Comment: ',fname);
  73.  loadfile(ans,'seektest.txt');
  74.  writeln(ans);
  75.  append(' (please check if the file has been deleted (below : FALSE =OK))',fname);
  76.   loadfile(ans,'seektest.txt');
  77.  writeln(ans);
  78.  DeleteFile(fname);
  79.  writeln(fileexists(fname));
  80.  writeln('Press return to end . . .');
  81.  readln;
  82.  end.
  83.  
  84.  


nugax

  • Full Member
  • ***
  • Posts: 232
Re: text (textfiles) type
« Reply #16 on: March 18, 2022, 09:19:41 pm »
Actually,

I was able to write a routine that closes the TInifile and writes the file, writes the comment, then opens the file back up. Easy enough to insert a comment.
-Nugax

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: text (textfiles) type
« Reply #17 on: March 18, 2022, 09:37:52 pm »
Actually,

I was able to write a routine that closes the TInifile and writes the file, writes the comment, then opens the file back up. Easy enough to insert a comment.

Yes, but you can't guarantee that the comment stays in the same place (or, for that matter, anywhere) after the next time the structured part of the file is written.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

nugax

  • Full Member
  • ***
  • Posts: 232
Re: text (textfiles) type
« Reply #18 on: March 18, 2022, 09:45:29 pm »
Your right... hmmm..

 except it doesnt write back to the inifile, just creates a default one. So, as of now, it only writes once.

but if I do make changes it will overwrite it, i bet.

-Nugax

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: text (textfiles) type
« Reply #19 on: March 18, 2022, 09:53:59 pm »
You'd be safest reimplementing with a TStringList... or even for a TStringList for the sections and a separate TStringList for the content of each section. You could potentially create a comment telling it that it was to try to stay after or before a particular entry, and save that as metadata.

I've got a couple of units which extend the standard TIniFile with array-like and record-like facilities...

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: text (textfiles) type
« Reply #20 on: March 19, 2022, 04:27:14 pm »
TextFiles is nonsense. The correct syntax is simply text. And the documentation is 100% correct (not 99.9).

While TextFiles indead does not exist, TextFile does, namely as an alias for the Text type. This alias was introduced with Delphi, cause Text is a common property name.

SEEK(0...) would probably overwrite the first few characters anyway (as in c)

Seek does not write anything, neither in Pascal nor in C.

 

TinyPortal © 2005-2018