Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Set file size without writing in it

Posted on 2004-03-24
8
Medium Priority
?
837 Views
Last Modified: 2010-04-04
It is possible to set file size when using TFileStream:

var f : tfilestream;
begin
f := tfilestream.create('somefile.dat',fmcreate 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.
0
Comment
Question by:ZhaawZ
[X]
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
8 Comments
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10672405
Yes, you can set the file size, even beyond what is actually there...

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(FileName), GENERIC_WRITE, 0, nil, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
  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;
0
 
LVL 11

Author Comment

by:ZhaawZ
ID: 10672735
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...
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10672742

What do you thing seek and truncate call behind the scenes???

Russell



0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Author Comment

by:ZhaawZ
ID: 10672920
Is there anything wrong?
0
 
LVL 1

Expert Comment

by:IrishFBall32
ID: 10695371
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.
0
 
LVL 11

Author Comment

by:ZhaawZ
ID: 10695798
Yes, I know ;) Actually I just reserve the necessary space on disk before writing the file and then write data to it.
0
 

Accepted Solution

by:
PashaMod earned 0 total points
ID: 12072844
Closed, 125 points refunded.
PashaMod
Community Support Moderator
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

618 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