Lazarus

Free Pascal => Beginners => Topic started by: JLWest on September 25, 2021, 09:36:05 am

Title: ReadFileToString
Post by: JLWest 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.
Title: Re: ReadFileToString
Post by: fred 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
Title: Re: ReadFileToString
Post by: ASerge 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)
Title: Re: ReadFileToString
Post by: fred 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.
Title: Re: ReadFileToString
Post by: ASerge 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;
Title: Re: ReadFileToString
Post by: Handoko 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 (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 (https://wiki.freepascal.org/End_of_Line).
Title: Re: ReadFileToString
Post by: Thaddy 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.
Title: Re: ReadFileToString
Post by: JLWest on September 25, 2021, 05:46:50 pm
Thanks All. ASerge code will do the trick.

 
TinyPortal © 2005-2018