ZhaawZ
asked on
Set file size without writing in it
It is possible to set file size when using TFileStream:
var f : tfilestream;
begin
f := tfilestream.create('somefi le.dat',fm create or fmopenwrite);
f.size := $1000000;
f.free
end;
Is it possible to do the same thing without using TFileStream? Example with using untyped file would be the best.
var f : tfilestream;
begin
f := tfilestream.create('somefi
f.size := $1000000;
f.free
end;
Is it possible to do the same thing without using TFileStream? Example with using untyped file would be the best.
ASKER
mm .. found it ..
var
f : file;
begin
assignfile(f,'d:\somefile. dat');
rewrite(f,1);
seek(f,$10000000);
truncate(f);
closefile(f);
end;
rllibby, thanx for the help, but it was without using untyped files ;)
No help needed anymore...
var
f : file;
begin
assignfile(f,'d:\somefile.
rewrite(f,1);
seek(f,$10000000);
truncate(f);
closefile(f);
end;
rllibby, thanx for the help, but it was without using untyped files ;)
No help needed anymore...
What do you thing seek and truncate call behind the scenes???
Russell
ASKER
Is there anything wrong?
It appears that you second example would work just fine, in fact i once wrote a similar program to test that my computer could actually address the full capacity of my harddrive.
The only thing to keep in mind though, if you take a file who's size is say 1MB, and change its size to 10MB there will be 9MB worth of "garbage" in the file from whatever was on the spots on the drive that the new size takes up. In other words it is similar to an un-initialized variable being full of the garbage from whatever was in RAM before it.
The only thing to keep in mind though, if you take a file who's size is say 1MB, and change its size to 10MB there will be 9MB worth of "garbage" in the file from whatever was on the spots on the drive that the new size takes up. In other words it is similar to an un-initialized variable being full of the garbage from whatever was in RAM before it.
ASKER
Yes, I know ;) Actually I just reserve the necessary space on disk before writing the file and then write data to it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Some code from a PAQ that I participated in. You can modify the function to just take a file handle, the real key to this are the calls to SetFilePointer / SetEndOfFile.
Hope this helps,
Russell
function HiLong(Value: Int64): LongWord;
begin
result:=((Value shr 32) and $FFFFFFFF);
end;
function LoLong(Value: Int64): LongWord;
begin
result:=LongWord(Value);
end;
function CreateJunkFile(FileName: String; Size: Int64): Boolean;
var hJunk: THandle;
dwLow: Integer;
dwHigh: Integer;
begin
// Get high/low
dwLow:=LoLong(Size);
dwHigh:=HiLong(Size);
// Create the new junk file
hJunk:=CreateFile(PChar(Fi
if (hJunk <> INVALID_HANDLE_VALUE) then
begin
// Set the file pointer
if (SetFilePointer(hJunk, dwLow, @dwHigh, FILE_BEGIN) = LoLong(Size)) then
// Set end of file
result:=SetEndOfFile(hJunk
else
// Failed to set file pointer
result:=False;
// Close the file
CloseHandle(hJunk);
end
else
// Failure
result:=False;
end;