Solved

SEEKING IN FILES BIGGER THAN 2 GIG

Posted on 2006-06-23
9
321 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Mdi application Child forms get behind control 7 216
Delphi selector screen 2 80
Find and Replace Stream with 0s 8 63
Unique identifier on a terminal server (rdp) 4 55
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

832 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