Delphi替代一些File I / O C ++ lib函数? [英] Delphi alternatives to some File I/O C++ lib functions?

查看:146
本文介绍了Delphi替代一些File I / O C ++ lib函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • fopen_s < - > OpenFile

  • fclose < - > CloseFile

我的假设是否正确?

我不知道最好使用什么,OpenFile或CreateFile。

I wonder what is better to use, OpenFile or CreateFile. The latter gives more freedom, but is it faster?

推荐答案

我不会在Delphi中使用 - 我会使用流。

I would use neither in Delphi – I would use streams. Low level file handling is messy and error-prone, it's much better to use higher level routines if you can.

你问哪个更快, OpenFile CreateFile 。它们基本上是一样的,但打开文件的任何方法都将映射到系统调用,所以性能将是相同的,无论你怎么做。更重要的是,打开文件的性能何时重要,在读取或写入时花费了时间。

You ask which is faster, OpenFile or CreateFile. They are basically the same, but any method of opening a file is going to map onto the system call anyway so the performance will be the same no matter how you do it. What's more, when does performance for opening a file matter, it's when reading or writing that time is expended.

任何关于性能的问题都很难在没有上下文的情况下回答。例如,读取数千个小文本文件的应用程序的答案不同于将备份流式传输到磁带驱动器的应用程序。

Any questions about performance are hard to answer without context. The answer for an app which reads thousands of small text files is different from one which streams backups to a tape drive, for example.

无论如何,利用Delphi提供的优秀的高级框架,使用流,避免低级I / O并享受!

Anyway, to stress my original point, take advantage of the excellent high-level framework that Delphi provides, use streams, avoid low-level I/O and enjoy!

那么,如何使用Delphi流呢?我将尝试用一个在一个字符串中将一些文本写入文件的示例来说明这一点。

So, how does one use a Delphi stream? I'll try to illustrate this with a made up example of writing some text, in a string, to a file.

procedure SaveTextToFile(FileName, Text: string);
var
  Stream: TFileStream;
begin
  Stream := TFileStream.Create(FileName, fmCreate);
  Try
    if Length(Text)>0 then
      Stream.WriteBuffer(Text[1], Length(Text)*SizeOf(Char));
  Finally
    Stream.Free;
  End;
end;

这是很自然的解释。 TFileStream构造函数的第二个参数确定文件模式。在这里,我们要创建一个全新的文件,所以如果有任何内容,它们会被删除。您也可以使用此参数指定文件共享。

It's pretty self-explanatory. The second parameter to the TFileStream constructor determines the file mode. Here we want to create a brand new file and so if any contents exist, they are removed. You can also specify file sharing with this parameter.

编写缓冲区的代码有一点模板,但又很简单。

The code to write the buffer out has a little boiler-plate but again is very simple.

加载它会产生一个几乎相同的例程:

Loading it back results in an almost identical routine:

function LoadTextFromFile(FileName: string): string;
var
  Stream: TFileStream;
begin
  Stream := TFileStream.Create(FileName, fmOpenRead);
  Try
    SetLength(Result, Stream.Size div SizeOf(Char));
    if Length(Result)>0 then
      Stream.ReadBuffer(Result[1], Length(Result)*SizeOf(Char));
  Finally
    Stream.Free;
  End;
end;

如果您希望查找文件,可以设置 Position 属性,或者调用 Seek()方法。后者的优点是,您可以从当前位置或结束位置寻求。

If you wish to seek around the file then you can set the Position property of the stream, or call the Seek() method. The advantage of the latter is that you can seek from current position or end position.

Streams是惯用的Delphi。它们普遍用于RTL和VCL以及第三方库。他们用本地Delphi方式用异常信号表示错误。有许多不同的流类都来自一个共同的祖先,许多例程接受这个共同的祖先。

Streams are idiomatic Delphi. They are used pervasively in the RTL and VCL and by 3rd party libraries. They signal errors with exceptions in the native Delphi manner. There are many different stream classes that all derive from a common ancestor and many routines accept this common ancestor.

这篇关于Delphi替代一些File I / O C ++ lib函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆