Forum > Beginners

ReadFileToString

(1/2) > >>

JLWest:
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.

fred:
In the past I could not find it so I write my own, very simple but it worked for me:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function WriteStringToFile(const filename, stringdata: string): boolean;var  f: TextFile;begin  Result := False;  AssignFile(f, filename);  Rewrite(f);  try    Write(f, stringdata);    Result := True;  finally    CloseFile(f);  end;end;
Edit: changesd code for ASerge remarks Reply #2

ASerge:

--- Quote from: fred on September 25, 2021, 11:10:25 am ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function WriteStringToFile(const filename, stringdata: string): boolean;var  f: TextFile;begin  Result := False;  AssignFile(f, filename);  try    Rewrite(f);    Write(f, stringdata);    Result := True;  finally    CloseFile(f);  end;end;
--- End quote ---
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:
Thanks ASerge,
I have only used it for debugging larger strings so I never had an error, it never gets into production code.

ASerge:

--- Quote from: 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?

--- End quote ---
I don't know such a function also. But the implementation is really not complicated. Here is an example using TFileStream:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses Classes; function ReadStringFromFile(const FileName: string): RawByteString;var  S: TFileStream;  Count: SizeInt;begin  S := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNoneFlags);  try    Count := S.Size;    SetLength(Result, Count);    S.ReadBuffer(Pointer(Result)^, Count);  finally    S.Free;  end;end; procedure WriteStringToFile(const FileName: string; const Data: RawByteString);var  S: TFileStream;begin  S := TFileStream.Create(FileName, fmCreate);  try    S.WriteBuffer(Pointer(Data)^, Length(Data));  finally    S.Free;  end;end;

Navigation

[0] Message Index

[#] Next page

Go to full version