Set EOF

How can I set EOF in the file open for binary ?
shvetsovAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mark2150Commented:
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
0
Bob LearnedCommented:
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.
0
shvetsovAuthor Commented:
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 ?
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

frankdCommented:
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.
0
mcriderCommented:
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!
0
mcriderCommented:
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!
0
Bob LearnedCommented:
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
0
shvetsovAuthor Commented:
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.
0
Bob LearnedCommented:
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.
0
mcriderCommented:
Believe it! There are only 2 ways to do it in VB... Either use the API that I gave you, or use one of the "read the file and write it again" solutions the other folks gave...

This is why VB allows API calls... Not "Everying" is built-in.


Cheers!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KDivadCommented:
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.
0
mcriderCommented:
KDivad,

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

M
0
mcriderCommented:
Glad I could help... Thanks for the points!


Cheers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.