Delphi 如何快速读写文件中的字符串


本文整理自网络,侵删。

 
const
  szChar = SizeOf(Char);
/// 

///   saves a string to a file
/// 
procedure StringToFile(const s: string; const FileName: string);
var
  FileStream: TFileStream;
begin
  FileStream := TFileStream.Create(FileName, fmCreate);
  try
    FileStream.WriteBuffer(Pointer(s)^, (Length(s) * szChar));
  finally
    FreeAndNil(FileStream);
  end; // try
end;

/// 

///   returns the content of the file as a string
/// 
function StringFromFile(const FileName: string): string;
var
  FileStream: TFileStream;
begin
  FileStream := TFileStream.Create(FileName, fmOpenRead);
  try
    SetLength(Result, (FileStream.Size div szChar));
    FileStream.ReadBuffer(Pointer(Result)^, FileStream.Size);
  finally
    FreeAndNil(FileStream);
  end; // try
end;

相关阅读 >>

Delphi 跟随鼠标位置点击弹出菜单

Delphi查找进程

Delphi idhttp上传图给asp完美解决

Delphi的对象注销方法destroy和free的区别

Delphi 如何屏蔽alt+f4

Delphi 取最前窗口标题(不重复)

Delphi实现ftp上传与下载

Delphi hook 指定程序窗体和控件的 wndproc

Delphi 写一个可拖动的 tshape

Delphi发送邮件

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...