Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

SEEKING IN FILES BIGGER THAN 2 GIG

Posted on 2006-06-23
9
322 Views
Last Modified: 2010-04-04
Hello All,

I have a routine that reads in a file and as it reads the file in it writes out an index file like

index is
tdelimit_struct = record
recnum:longint;
spos:int64;
ssize:int64;
skey:array[1..10] of char;
end;

(* spos = start position in file, ssize is no of characters to read in *)

Once the data is read in a third party dll sorts the data and then supplies me back just the record no's i need for the sorted
data.

This is why the index is written as the data coming in can be delimitted or fixed field.

To read the data back in I have a tfilestream method....

seekf=tfilestream.create(ifile,fmopenread);
repeat
.
(* get record from sort , read sort key from main index then*)
.
(* and to seek a record *)
seekf.seek(delimit_struct.spos,soFromBeginning);
seekf.read(buffer,delimit_struct.ssize);

-- PROBLEM HERE ---------
The problem is when the file is over 2 gig i get a range check error on the seek - despite the fact i have a int64?

The offset crashing it was 2152430441 (which  is over 2gig).

I can seek seekf is meant to read over 2gig files?

I had similar problems with getting the size of the original files but fond code to fix this but the read is killing it?

HELP please!

This is in delphi7.

Thanks

Phil


0
Comment
Question by:PHILFRED
  • 4
  • 2
9 Comments
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16969653
Try
seekf.Position := delimit_struct.spos;
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16969781
Or use SetFilePointer win32 api function
e.g.
SetFilePointer(seekf.Handle, delimit_struct.spos, nil, FILE_BEGIN);
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16970100
Sorry the above example will not work with file >2 gb. use the following instead.

var
  MyRec : packed record L, H : cardinal end absolute i;
begin
  // your code
  SetFilePointer(fs.Handle, MyRec.L, @MyRec.H, FILE_BEGIN);
end;
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:PHILFRED
ID: 16970198
Thanks Atul - but how do i get the values into MyRec?

var
  MyRec : packed record L, H : cardinal end absolute i;
begin
  // your code
  SetFilePointer(fs.Handle, MyRec.L, @MyRec.H, FILE_BEGIN);
end;
0
 
LVL 10

Accepted Solution

by:
atul_parmar earned 125 total points
ID: 16970326
Phil, Sorry abt that!

var
  i : int64
  MyRec : packed record L, H : cardinal end absolute i;
begin
  // your code
  i := delimit_struct.spos;
  SetFilePointer(fs.Handle, MyRec.L, @MyRec.H, FILE_BEGIN);
  // because we appended the absolute to the MyRec it will point to the same location where i variable resides so will be initialized when we assign delimit_struct.spos to i (i.e. i := delimit_struct.spos;)
end

I hope, that will do. I m moving now. Happy Week end. :)

Atul.
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 16970388
You can also skip the absolute mapping by using LARGE_INTEGER casting, eg:

Regards,
Russell

function GetSizeEx(Handle: THandle): Int64;
begin

  LARGE_INTEGER(result).LowPart:=GetFileSize(Handle, @LARGE_INTEGER(result).HighPart);

end;

procedure SetSizeEx(Handle: THandle; Size: Int64);
begin

  SetFilePointer(Handle, LARGE_INTEGER(Size).LowPart, @LARGE_INTEGER(Size).HighPart, FILE_END);
  Win32Check(SetEndOfFile(Handle));

end;

function SeekEx(Handle: THandle; Offset: Int64; Origin: Word): Int64;
begin

  result:=Offset;
  LARGE_INTEGER(result).LowPart:=SetFilePointer(Handle, LARGE_INTEGER(result).LowPart, @LARGE_INTEGER(result).HighPart, Origin);
  if (LARGE_INTEGER(result).LowPart = $FFFFFFFF) then RaiseLastWin32Error;

end;
0
 

Author Comment

by:PHILFRED
ID: 16970562
Ok Thanks all i will implement some code over the weekend and see what works!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question