CreateFilledFile (CreateSizedFile)

Share your advanced PureBasic knowledge/code with the community.
b1be
User
User
Posts: 25
Joined: Sat Mar 19, 2005 5:47 pm

CreateFilledFile (CreateSizedFile)

Post by b1be »

Code updated For 5.20+

Code: Select all

Procedure CreateFilledFile(Name.s,fsize.q)
  CreateFile(0,Name)
  FileSeek(0,fsize-1)
  WriteByte(0,0)
  CloseFile(0)
EndProcedure
Test

Code: Select all

CreateFilledFile("Temp.temp",123)
Should create "Temp.temp" with exact size of 123Bytes

dont know if was posted/or was in help ... but it's a Goodie
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

As the PB manual said, you must check the result of CreateFile().
No offense, it's just cleaner this way. :wink:

Code: Select all

Procedure CreateFilledFile(Name.s, fsize.q)
  Protected file.l = CreateFile(#PB_Any, Name)
  If file
    FileSeek(file, fsize-1)
    WriteByte(file, 0)
    CloseFile(file)
    ProcedureReturn #True
  EndIf
EndProcedure
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
b1be
User
User
Posts: 25
Joined: Sat Mar 19, 2005 5:47 pm

Post by b1be »

none taken ... i still find this usefull tip.. and thanks for posting

Code: Select all

Procedure CreateFilledFile(Name.s, fsize.q) 
  Protected file.l = CreateFile(#PB_Any, Name) 
  If file 
    If fsize<=0
      CloseFile(file)
      ProcedureReturn #True 
    Else
      FileSeek(file, fsize-1) 
      WriteByte(file, 0) 
      CloseFile(file) 
    ProcedureReturn #True 
   EndIf
  EndIf 
EndProcedure
fixed a bug with filesize=0
Bonne_den_kule
Addict
Addict
Posts: 841
Joined: Mon Jun 07, 2004 7:10 pm

Post by Bonne_den_kule »

This one is much faster...

Code: Select all

Procedure CreateFilledFile (Filename.s, NewLength.q)
  Define Filehandle.l
  Filehandle = CreateFile_(@Filename, #GENERIC_READ|#GENERIC_WRITE,#FILE_SHARE_READ|#FILE_SHARE_WRITE, #Null, #CREATE_ALWAYS, #FILE_ATTRIBUTE_NORMAL, #Null) 
  If Filehandle
    If SetFilePointer_(Filehandle, NewLength, (@NewLength)+4, #FILE_BEGIN ) 
      If SetEndOfFile_(Filehandle) 
        If CloseHandle_(Filehandle)
        Else 
          ProcedureReturn 0
        EndIf
      Else
        ProcedureReturn 0
      EndIf
    Else
      ProcedureReturn 0
    EndIf 
  Else
    ProcedureReturn 0
  EndIf 
  ProcedureReturn 1
EndProcedure
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

Bonne_den_kule wrote:This one is much faster...

Code: Select all

Procedure CreateFilledFile (Filename.s, NewLength.q)
  Define Filehandle.l
  Filehandle = CreateFile_(@Filename, #GENERIC_READ|#GENERIC_WRITE,#FILE_SHARE_READ|#FILE_SHARE_WRITE, #Null, #CREATE_ALWAYS, #FILE_ATTRIBUTE_NORMAL, #Null) 
  If Filehandle
    If SetFilePointer_(Filehandle, NewLength, (@NewLength)+4, #FILE_BEGIN ) 
      If SetEndOfFile_(Filehandle) 
        If CloseHandle_(Filehandle)
        Else 
          ProcedureReturn 0
        EndIf
      Else
        ProcedureReturn 0
      EndIf
    Else
      ProcedureReturn 0
    EndIf 
  Else
    ProcedureReturn 0
  EndIf 
  ProcedureReturn 1
EndProcedure
Indeed, it's faster, at least when you need it on a Windows box; unfortunately, faster code most of the time is less portable....
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
User avatar
bingo
Enthusiast
Enthusiast
Posts: 210
Joined: Fri Apr 02, 2004 12:21 pm
Location: germany/thueringen
Contact:

Post by bingo »

@dell_jockey

Debug CreateFilledFile("e:\dummy.tmp",$FFFFFFFFF) 8)

Debug CreateFilledFile("e:\dummy.tmp",$FFFFFFFFFF) :shock:

any limitations of SetFilePointer ???

[xp/sp2/ntfs]
["1:0>1"]
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Platform SDK wrote:The SetFilePointer function moves the file pointer of an open file.

This function stores the file pointer in two LONG values. To work with file pointers that are larger than a single LONG value, it is easier to use the SetFilePointerEx function.
BERESHEIT
Bonne_den_kule
Addict
Addict
Posts: 841
Joined: Mon Jun 07, 2004 7:10 pm

Post by Bonne_den_kule »

bingo wrote:@dell_jockey

Debug CreateFilledFile("e:\dummy.tmp",$FFFFFFFFF) 8)

Debug CreateFilledFile("e:\dummy.tmp",$FFFFFFFFFF) :shock:

any limitations of SetFilePointer ???

[xp/sp2/ntfs]
Why are you trying to make a 1000 GB file ? :shock:
Post Reply