Recent

Author Topic: ReadFileToString  (Read 954 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1054
ReadFileToString
« on: September 25, 2021, 09:36:05 am »
The is a function that will read a file into a string 'ReadFileToString'.

Is there a WriteStringToFile? I can't find it but maybe it's called something else?

Thanks.
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

fred

  • Full Member
  • ***
  • Posts: 193
Re: ReadFileToString
« Reply #1 on: September 25, 2021, 11:10:25 am »
In the past I could not find it so I write my own, very simple but it worked for me:

Code: Pascal  [Select][+][-]
  1. function WriteStringToFile(const filename, stringdata: string): boolean;
  2. var
  3.   f: TextFile;
  4. begin
  5.   Result := False;
  6.   AssignFile(f, filename);
  7.   Rewrite(f);
  8.   try
  9.     Write(f, stringdata);
  10.     Result := True;
  11.   finally
  12.     CloseFile(f);
  13.   end;
  14. end;

Edit: changesd code for ASerge remarks Reply #2
« Last Edit: September 25, 2021, 11:57:14 am by fred »
Lazarus 2.0.12 / FPC 3.2.0 Windows 10

ASerge

  • Hero Member
  • *****
  • Posts: 1869
Re: ReadFileToString
« Reply #2 on: September 25, 2021, 11:41:30 am »
Code: Pascal  [Select][+][-]
  1. function WriteStringToFile(const filename, stringdata: string): boolean;
  2. var
  3.   f: TextFile;
  4. begin
  5.   Result := False;
  6.   AssignFile(f, filename);
  7.   try
  8.     Rewrite(f);
  9.     Write(f, stringdata);
  10.     Result := True;
  11.   finally
  12.     CloseFile(f);
  13.   end;
  14. end;
Assign does not open the file handle. I. e. the try must be after Rewrite. Otherwise, if there is an error in the Rewrite procedure, another error may occur in the Close procedure (close a handle that is not open)

fred

  • Full Member
  • ***
  • Posts: 193
Re: ReadFileToString
« Reply #3 on: September 25, 2021, 11:59:27 am »
Thanks ASerge,
I have only used it for debugging larger strings so I never had an error, it never gets into production code.
Lazarus 2.0.12 / FPC 3.2.0 Windows 10

ASerge

  • Hero Member
  • *****
  • Posts: 1869
Re: ReadFileToString
« Reply #4 on: September 25, 2021, 12:10:38 pm »
The is a function that will read a file into a string 'ReadFileToString'.
Is there a WriteStringToFile? I can't find it but maybe it's called something else?
I don't know such a function also. But the implementation is really not complicated. Here is an example using TFileStream:
Code: Pascal  [Select][+][-]
  1. uses Classes;
  2.  
  3. function ReadStringFromFile(const FileName: string): RawByteString;
  4. var
  5.   S: TFileStream;
  6.   Count: SizeInt;
  7. begin
  8.   S := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNoneFlags);
  9.   try
  10.     Count := S.Size;
  11.     SetLength(Result, Count);
  12.     S.ReadBuffer(Pointer(Result)^, Count);
  13.   finally
  14.     S.Free;
  15.   end;
  16. end;
  17.  
  18. procedure WriteStringToFile(const FileName: string; const Data: RawByteString);
  19. var
  20.   S: TFileStream;
  21. begin
  22.   S := TFileStream.Create(FileName, fmCreate);
  23.   try
  24.     S.WriteBuffer(Pointer(Data)^, Length(Data));
  25.   finally
  26.     S.Free;
  27.   end;
  28. end;

Handoko

  • Hero Member
  • *****
  • Posts: 4346
  • My goal: build my own game engine using Lazarus
Re: ReadFileToString
« Reply #5 on: September 25, 2021, 12:13:17 pm »
Old school file saving/loading demos can be found here:
https://wiki.freepascal.org/Portal:HowTo_Demos#File_handling

Binary file
File that contains non text characters. Usually you use record to define the sizes of each fields.

Text file
File that contains text only, each lines are separated using end of line marker.

Thaddy

  • Hero Member
  • *****
  • Posts: 10929
Re: ReadFileToString
« Reply #6 on: September 25, 2021, 12:26:31 pm »
I don't know such a function also. But the implementation is really not complicated. Here is an example using TFileStream:
Example is in the KOL library, using its own streams, but very close to what you showed.
The average programmer productivity is 4-5 hours per day. Peak performance 72 hours for short bursts. MTBF is 1 second or less.

JLWest

  • Hero Member
  • *****
  • Posts: 1054
Re: ReadFileToString
« Reply #7 on: September 25, 2021, 05:46:50 pm »
Thanks All. ASerge code will do the trick.

 
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

 

TinyPortal © 2005-2018