中国程序员联盟 正在重新改版中ing 不便之处还请见谅 改版后将内容涉及java delphi .net php
 
  首页 | 数据库开发 | 网络通讯 | 多线程 | 多媒体开发 | 图像处理 | 程序人生 | 系统函数 | 控件开发 | Web服务
 
  当前位置:笨鱼delphi技术网>系统函数>文章内容

delphi Delphi如何处理UNIX文件存储

来源:国外 关于:Stefan Pettersson 发布时间:2007-07-19   [收藏] [推荐]
First of all, the reason is because Windows uses CRLF ($0D $0A or #13 #10) and UNIX/Linux uses just LF ($0A or #10) as linebreaks in textfiles.
The need to do it is because when using the Readln procedure it will not work on UNIX files because it cannot detect the linebreak. Instead of seeing your application go crazy it might be a nice thing to detect if it's a UNIX file or not in advance, and then provide the option to convert it if necessary.
The way to detect if it's a UNIX or Windows file is to spot the difference, i.e. to see if a CR char precedes the LF char.
Here is a go at it:
function IsFileUNIX(Filename: string): boolean;
var
  StopRead: boolean;
  F: file of Byte;
  CurB, PrevB: Byte;
begin
  StopRead := False;
  PrevB := 0;
  Result := True;
  AssignFile(F, Filename);
  FileMode := 0; // read only
  Reset(F);
  while (not Eof(F)) and (StopRead = False) do
  begin
    Read(F, CurB);
    // check if $0D precedes $0A
    if CurB = $0A then
    begin
      Result := PrevB <> $0D;
      StopRead := True;
    end;
    PrevB := CurB;
  end;
end;
Well, this function did what I wanted, however, I thought it looked kind of ugly so I began to think a little bit how I may use the same principle, but execute it with fewer statements and make the function a little bit more beautiful.
Simply replacing the while loop with a repeat loop did miracles, here's the second go at it:
function IsFileUNIX2(Filename: string): boolean;
var
  F: file of Byte;
  CurB, PrevB: Byte;
begin
  AssignFile(F, Filename);
  FileMode := 0; // read only
  Reset(F);
  repeat
    PrevB := CurB;
    Read(F, CurB);
  until (CurB = $0A) or (Eof(F));
  // check if $0D precedes $0A
  Result := PrevB <> $0D;
end;

[浏览: 次]   
上一篇:delphi 如何快速读取文本文件   下一篇:delphi 在GRID画一个没有滚动条的背景图
[收藏] [推荐] [返回顶部] [打印本页] [关闭窗口]  
    评论加载中…
google adsense热点文章
·delphi Delphi中ShellExecute的妙用
·delphi 如何快速读取文本文件
·delphi 如何判断输入值是否中文
·delphi 在应用层截获键盘消息
·delphi delphi实现服务开启与关闭
·delphi 实时记录事件日志
·delphi 使MEMO自动滚动
·delphi 如何区分键盘两个Enter键
·delphi 切换界面的方法
·delphi 汉字输入法的编程及使用
·delphi Delphi程序输入法自动切换最简
·delphi 消息是由谁来发出又由谁来完成
     delphi技术网 | firefox 下载 | Avant Browser下载 | dedecms 技术网 | drupal 爱好者 | php 技术网
  Copyright@www.delphichm.com,2006-2009.All Rights Reserved.
 
程序员联盟 | delphi Java .net|QQ:707102932