Link to home
Start Free TrialLog in
Avatar of shvetsov
shvetsov

asked on

Set EOF

How can I set EOF in the file open for binary ?
Avatar of mark2150
mark2150

Ummm, don't really understand what you're after here...

If you read a block of data from a binary file and you're at the end then then length of the returned string will be shorter than the original buffer.

M
Avatar of Bob Learned
If you are trying to detect EOF with binary files, then you would have to use the LOF() function to look at the length of the file.
Avatar of shvetsov

ASKER

No.
I mean I've opened a large file (10K).
And I want to write only 1K to it.
And other 9K should be truncated.
How can I do it ?
Open the original 10K file.
Read in 1K.
Close original file
Delete or Rename original file.
Open new file
Write 1K to new file
Close new file.

This is one way of doing it.
You need to do this with APIs.  Here's an example:

Put this in a module:

' OpenFile() Flags
Global Const OF_READ = &H0
Global Const OF_WRITE = &H1
Global Const OF_READWRITE = &H2
Global Const OF_SHARE_COMPAT = &H0
Global Const OF_SHARE_EXCLUSIVE = &H10
Global Const OF_SHARE_DENY_WRITE = &H20
Global Const OF_SHARE_DENY_READ = &H30
Global Const OF_SHARE_DENY_NONE = &H40
Global Const OF_PARSE = &H100
Global Const OF_DELETE = &H200
Global Const OF_VERIFY = &H400
Global Const OF_CANCEL = &H800
Global Const OF_CREATE = &H1000
Global Const OF_PROMPT = &H2000
Global Const OF_EXIST = &H4000
Global Const OF_REOPEN = &H8000

' OpenFile() Structure
Type OFSTRUCT
   cBytes As String * 1
   fFixedDisk As String * 1
   nErrCode As Integer
   reserved As String * 4
   szPathName As String * 128
End Type
Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Declare Function llseek Lib "kernel32" Alias "_llseek" (ByVal hFile As Long, ByVal lOffset As Long, ByVal iOrigin As Long) As Long
Declare Function SetEndOfFile Lib "kernel32" (ByVal hFile As Long) As Long
Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long


Then you can use this code in a command button:

Private Sub Command1_Click()
    Dim fileStruct As OFSTRUCT
    Dim vFNUM As Integer
    Dim FileName As String
    Dim vSZ As Long
   
    FileName = "C:\Windows\Desktop\eoftest.txt"
    vSZ = 10
    vFNUM = OpenFile(FileName, fileStruct, OF_READWRITE)
    llseek vFNUM, vSZ, 0
    SetEndOfFile vFNUM
    lclose vFNUM
End Sub


Use NOTEPAD to create the file C:\Windows\Desktop\eoftest.txt with 20 characters in it then run the program.

Look at the file after it runs... There will only be 10 characters in the file!


Cheers!
Using my method, you don't need to use frankd's method of opening two files...

Just figure out where you want the EOF marker in the file and use the code I gave you to put it there.


Cheers!
Public Sub Main()

   ReadBinaryFile "d:\temp\gm.dls", 100
   
End Sub

Private Sub ReadBinaryFile(sFileName As String, Optional lTruncate As Long = -1)

Dim bytChar() As Byte
Dim iHandle As Integer

   iHandle = FreeFile()

   Open sFileName For Binary Access Read As iHandle
   
   ReDim bytChar(LOF(iHandle))
   
   Get iHandle, , bytChar
   
   Close iHandle
   
   If lTruncate <> -1 Then
      ReDim bytChar(lTruncate - 1)
     
      Kill sFileName
     
      Open sFileName For Binary Access Write As iHandle
     
      Put iHandle, , bytChar
     
      Close iHandle
   End If
   
End Sub
Gentlemen, I know the Win32 API.

But this area is called "Visual Basic". And I can't belive that VB have no statement for this purpose.

I will grade mcirider's answer as "excellent" if no positive answer will come in 24 hours.
As with many other facets of VB, you are going to have to believe it, because there is no one statement that will set and EOF marker on a file.
ASKER CERTIFIED SOLUTION
Avatar of mcrider
mcrider

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can retrieve the number of bytes you want from the file, close it, open it for output, close it, reopen for binary and write the bytes back out. Still a clumsy method, but opening a file for output will set it's length to zero.
KDivad,

your comment is yet another "read the file and write it again" solution.
I know that. But it is a slight variation of the original that doesn't require any API's.
Ok, mcireader, you are the winner :)
please make a new answer so I can grade it.
Just use the "Accept comment as answer" feature...

M
Glad I could help... Thanks for the points!


Cheers!