首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

新手,请问一个读取文本文件的有关问题

2012-12-20 
新手,请教一个读取文本文件的问题现有一个log文件,内容大致如下:2012-09-29 10:34:51INFO: 时间戳:Wed

新手,请教一个读取文本文件的问题
现有一个log文件,内容大致如下:
2012-09-29 10:34:51==>INFO: 时间戳:Wed Nov 18 09:56:00 2011
2012-09-29 10:34:51==>INFO: 
----------------------------------
2012-09-29 10:35:08==>D: 开始
2012-09-29 10:35:08==>D: Input= 2140^401012^1057^^|^
2012-09-29 10:35:08==>D: InputPara:2140^401012^1057^^|^
2012-09-29 10:35:09==>D: Out:返回值为:0,数据为:201209291035391590^71077247|^
2012-09-29 10:35:09==>D: 结束
----------------------------------
2012-09-29 10:35:15==>: 开始
2012-09-29 10:35:15==>D: Input= 2210^401012^1057^^|^
2012-09-29 10:35:15==>D: InputParam:2210^401012^1057^^|^
2012-09-29 10:35:15==>D: Out:返回值为:-1,数据为:无效的输入参数
2012-09-29 10:35:15==>D: 结束

请教怎么样处理得到读取文本文件后返回结果:
2140^401012^1057^^|^
成功
2210^401012^1057^^|^
失败
[最优解释]
说清楚点。。没看懂
[其他解释]
返回值0表示成功,非0表示失败?
[其他解释]
没看懂……什么意思?
[其他解释]
是取 Input= 2140^401012^1057^^
[其他解释]
^后的值吧,题目真看不怎么明白。
[其他解释]
调入内存,一行一行地读取。用POST()函数查找有用的行,读取相关信息即可。
当前行中包含'input='就取它的后20个字符。
当前行中包含'Out:返回值为:'就取它面的后1位,如果为0则成功,否则就不成功。

[其他解释]
贴个delphi5开发人员指南的例子给你
uses MemMap
//以下为MemMap代码
unit MemMap;

interface

uses Windows,SysUtils,Classes;

type
  EMMFError = class(Exception);

  TMemMapFile = class
  private
    FFileName: string;
    FSize: LongInt;
    FFileSize: LongInt;
    FFileMod: Integer;
    FFileHandle: Integer;
    FMapHandle: Integer;
    FData: PByte;
    FMapNow: Boolean;

    procedure AllocFileHandle;
    procedure AllocFileMapping;
    procedure AllocFileView;
    function GetSize: LongInt;
  public
    constructor Create(FileName: string;FileMod: Integer;Size: Integer;MapNow: Boolean);virtual;
    destructor Destroy;override;
    procedure FreeMapping;
    property Data: PByte read FData;
    property Size: LongInt read FSize;
    property FileName: string read FFileName;
    property FileHandle: Integer read FFileHandle;
    property MapHandle: Integer read FMapHandle;
  end;

implementation

{ TMemMapFile }

procedure TMemMapFile.AllocFileHandle;
begin
  if FFileMod = fmCreate then
    FFileHandle := FileCreate(FFileName)
  else


    FFileHandle := FileOpen(FFileName,FFileMod);

  if FFileHandle = 0 then
    raise EMMFError.Create('Failed to open or create file');
end;

procedure TMemMapFile.AllocFileMapping;
var
  ProtAttr: DWORD;
begin
  if FFileMod = fmOpenRead then
    ProtAttr := PAGE_READONLY
  else
    ProtAttr := PAGE_READWRITE;

  FMapHandle := CreateFileMapping(FFileHandle,nil,ProtAttr,0,FSize,nil);

  if FMapHandle = 0 then
    raise EMMFError.Create('Failed to create file mapping');
end;

procedure TMemMapFile.AllocFileView;
var
  Access: LongInt;
begin
  if FFileMod = fmOpenRead then
    Access := FILE_MAP_READ
  else
    Access := FILE_MAP_ALL_ACCESS;

  FData := MapViewOfFile(FMapHandle,Access,0,0,FSize);

  if FData = nil then
    raise EMMFError.Create('Failed to map view of file');
end;

constructor TMemMapFile.Create(FileName: string; FileMod, Size: Integer;
  MapNow: Boolean);
begin
  FFileName := FileName;
  FFileMod := FileMod;
  FMapNow := MapNow;

  AllocFileHandle;

  FFileSize := GetFileSize(FFileHandle,nil);
  FSize := Size;

  try
    AllocFileMapping;
  except
    on EMMFError do
    begin
      CloseHandle(FFileHandle);
      FFileHandle := 0;
      raise;
    end;
  end;

  if FMapNow then
    AllocFileView;
end;

destructor TMemMapFile.Destroy;
begin
  if FFileHandle <> 0 then
    CloseHandle(FFileHandle);

  if FMapHandle <> 0 then
    CloseHandle(FMapHandle);

  FreeMapping;
  inherited Destroy;
end;

procedure TMemMapFile.FreeMapping;
begin
  if FData <> nil then
  begin
    UnmapViewOfFile(FData);
    FData := nil;
  end;
end;

function TMemMapFile.GetSize: LongInt;
begin
  if FSize <> 0 then
    Result := FSize
  else
    Result := FFileSize;
end;

end.
//引用单元到此为止
//以下为查找内容
var
  MemMapFile: TMemMapFile;
  FounStr: PChar;
  FName: string;
  FindString: string;
begin
  FName := 'C:\aa.log';//你要查找的文件全名
  MemMapFile := TMemMapFile.Create(FName,fmOpenRead,0,True);
  try
    FindString := 'aaaa'; //你要找的字符
    FounStr := StrPos(PChar(MemMapFile.Data),PChar(FindString));


    if FounStr <> nil then
    begin
      //说明找到了
    end;
  finally
    MemMapFile.Free;
  end;

end;

热点排行