Recent

Author Topic: ReadFileToString  (Read 6452 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 19613
  • Glad to be alive.
Re: ReadFileToString
« Reply #15 on: December 30, 2025, 02:21:17 pm »
There is no race condition and there is no need to determine the file type.
How would you create a race condition in sequential code? There is no threading involved.
How can you determine the file type without relying on "extensions" that really have no meaning on many platforms?

Disturbingly Silly remark again. Trolling.

BTW: because of the way the load mechanism works, this code would also happily load an executable file which contents is perfectly legal because how Pascal AnsiStrings work . Looks funny if you print it, but the content is a bytewise representation of the file. It is not uncommon to use it in e.g. a hex editor.

Just provide better code. The above code is around since 1996.

And almost everybody knows that code in some way or other.
[edit] almost forgot full program:
Code: Pascal  [Select][+][-]
  1. program ss;
  2. {$ifdef fpc}{$mode objfpc}{$H+}{$endif}
  3. uses classes,sysutils;
  4.  
  5. function StrLoadFromFile(const aFile:string):string;
  6. begin
  7.   Result := '';
  8.   if FileExists(aFile) then
  9.   with TStringList.Create do
  10.   try
  11.      LoadFromFile(aFile);
  12.      Result := Text;
  13.   finally
  14.     free;
  15.   end;
  16. end;
  17. begin
  18.   Writeln(StrLoadFromFile('ss.pas'));
  19. end.
Silly you...  >:D O:-) :P
« Last Edit: December 30, 2025, 03:27:26 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

jamie

  • Hero Member
  • *****
  • Posts: 7892
Re: ReadFileToString
« Reply #16 on: December 30, 2025, 09:34:06 pm »
Actually, I think the argument here is that FileExists tells you the file is there but is it ready for you to process or has it been locked, currently being processed by another task?

 Using "FindFirst" will allow you to identify the file and at the same time check to see it's available for you to process.

Jamie
The only true wisdom is knowing you know nothing

zamtmn

  • Hero Member
  • *****
  • Posts: 689
Re: ReadFileToString
« Reply #17 on: December 30, 2025, 09:50:52 pm »
Code: Pascal  [Select][+][-]
  1. with TStringList.Create do
  2.   try
  3.      LoadFromFile(aFile);
This code does a lot of unnecessary things. it tries to control the encodings, splits the text into lines, and glues the lines back together. Just allocate memory and read the file there
« Last Edit: December 30, 2025, 09:52:30 pm by zamtmn »

jamie

  • Hero Member
  • *****
  • Posts: 7892
Re: ReadFileToString
« Reply #18 on: December 30, 2025, 10:12:36 pm »
Yes, that is why they call if a "LIST"  :D

jamie
The only true wisdom is knowing you know nothing

creaothceann

  • Sr. Member
  • ****
  • Posts: 425
Re: ReadFileToString
« Reply #19 on: December 30, 2025, 10:42:06 pm »
There is no race condition [...] How would you create a race condition in sequential code? There is no threading involved
By using an OS that allows more than one program to run at once.


there is no need to determine the file type [...] How can you determine the file type without relying on "extensions" that really have no meaning on many platforms?
The only one talking about file types is you.


Silly you...  >:D O:-) :P
Alright, seems like it's finally time for me to block this forum's smileys in my browser.

Thaddy

  • Hero Member
  • *****
  • Posts: 19613
  • Glad to be alive.
Re: ReadFileToString
« Reply #20 on: January 01, 2026, 04:16:24 pm »
You did not do yourself a favor in responding to my reply, it looks really funny.
Baseless nonsense again. AND no code again. TROLL.

For good measure: File IO is safe on almost all systems that support multithreading, like Windows or Unix. Unless "race" in your "race condition" refers to skin color or something?
The Trumpian way out?
« Last Edit: January 01, 2026, 04:19:11 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

creaothceann

  • Sr. Member
  • ****
  • Posts: 425
Re: ReadFileToString
« Reply #21 on: January 01, 2026, 08:40:28 pm »
You did not do yourself a favor in responding

Don't worry, won't happen again; added you to my ignore list.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12634
  • Debugger - SynEdit - and more
    • wiki
Re: ReadFileToString
« Reply #22 on: January 01, 2026, 10:06:55 pm »
As a moderator (on the last half dozen posts):
All parties, please calm down.




Me personal: On the technical part, yes there is a race.

"FileExists" may return true, but by the time the process tries to read the file, the file by that may have been removed by another process. And yes, its unlikely as hell for that to happen in the little time that there is, but that is exactly the definition of race...


On the other hand: There being a race is not a problem. It is an acceptable risk, and can be handled (together with that the target is not readable and other errors).

LemonParty

  • Hero Member
  • *****
  • Posts: 615
Re: ReadFileToString
« Reply #23 on: August 04, 2026, 04:06:00 pm »
In FPC 3.3.1 there possible to read a whole file content into string by this code:
Code: Pascal  [Select][+][-]
  1. uses
  2.   System.IOUtils;
  3.  
  4. var
  5.   FileContent: string;
  6. begin
  7.   FileContent := TFile.ReadAllText('C:\path\to\your\file.txt');
  8. end;
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

 

TinyPortal © 2005-2018