removing eof

  • Thread starter Thread starter Rhona Mandool via AccessMonster.com
  • Start date Start date
R

Rhona Mandool via AccessMonster.com

I need any suggestions that you may have with respect to using VB codes to
remove End of File marker/pointer.

I need to Open the file, Go to End of file and remove the EOF then save the
text file.

Thanks
 
I need any suggestions that you may have with respect to using VB codes to
remove End of File marker/pointer.

I need to Open the file, Go to End of file and remove the EOF then save the
text file.

You can't actually do this with a text file in Windows. Instead you have
to read the entire file and write it all back to disk except for the
last character. The StripEOF function below will probably do this
("probably" because I haven't tested it thoroughly) as long as the files
are small enough to read into memory in one chunk (i.e. not more than a
few megabytes). It uses a couple of my library functions which I've also
pasted below.

If the files are really big, you need to write code that reads and
writes them in manageable chunks, identifying the last chunk and
removing the EOF before writing it to disk.

Function StripEOF(FileSpec As String) As Boolean
'Removes EOF character
'Returns True if successful or no EOF; False on error

Dim EOF As String
Dim strContents As String
Dim ErrCode As Long

EOF = Chr(&H1A)
ErrCode = 0
strContents = FileContentsAsString(FileSpec, False, ErrCode)
If ErrCode = 0 Then
'File has been read
If Right(strContents, Len(EOF)) = EOF Then
'EOF found: remove it
strContents = Left(strContents, Len(strContents) - Len(EOF))
End If
ErrCode = WriteToFile(strContents, FileSpec, True)
End If
StripEOF = (ErrCode = 0)

End Function




Function FileContentsAsString(FileSpec As Variant, _
Optional ReturnErrors As Boolean = False, _
Optional ByRef ErrCode As Long) As Variant
'By John Nurick 2003-05
'Retrieves contents of file as a string
'Silently returns Null on error unless
' ReturnErrors is true, in which case
' uses CVErr() to return an error value.
' Optionally, you can retrieve the error
' code in the ErrCode argument

Dim lngFN As Long

On Error GoTo Err_FileContents
If IsNull(FileSpec) Then
FileContentsAsString = Null
Else
lngFN = FreeFile()
Open FileSpec For Input As #lngFN
FileContentsAsString = Input(LOF(lngFN), #lngFN)
Close #lngFN
End If
ErrCode = 0
GoTo Exit_FileContents

Err_FileContents:
ErrCode = Err.Number
If ReturnErrors Then
FileContentsAsString = CVErr(Err.Number)
Else
FileContentsAsString = Null
End If
Err.Clear
Exit_FileContents:
Close #lngFN
End Function




Function WriteToFile(Var As Variant, _
FileSpec As String, _
Optional Overwrite As Long = True) _
As Long
'Writes Var to a textfile as a string.
'Returns 0 if successful, an errorcode if not.
'By John Nurick

'Overwrite argument controls what happens
'if the target file already exists:
' -1 or True (default): overwrite it.
' 0 or False: append to it
' Any other value: abort.

Dim lngFN As Long

On Error GoTo Err_WriteToFile
lngFN = FreeFile()
Select Case Overwrite
Case -1 'True
Open FileSpec For Output As #lngFN
Case 0 'False
Open FileSpec For Append As #lngFN
Case Else
If Len(Dir(FileSpec)) > 0 Then
Err.Raise 58 'File already exists
Else
Open FileSpec For Output As #lngFN
End If
End Select
Print #lngFN, CStr(Nz(Var, ""));
Close #lngFN
WriteToFile = 0
Exit Function
Err_WriteToFile:
WriteToFile = Err.Number
End Function
 
Might I ask why you need to remove the EOF?

John said:
You can't actually do this with a text file in Windows. Instead you have
to read the entire file and write it all back to disk except for the
last character. The StripEOF function below will probably do this
("probably" because I haven't tested it thoroughly) as long as the files
are small enough to read into memory in one chunk (i.e. not more than a
few megabytes). It uses a couple of my library functions which I've also
pasted below.

If the files are really big, you need to write code that reads and
writes them in manageable chunks, identifying the last chunk and
removing the EOF before writing it to disk.

Function StripEOF(FileSpec As String) As Boolean
'Removes EOF character
'Returns True if successful or no EOF; False on error

Dim EOF As String
Dim strContents As String
Dim ErrCode As Long

EOF = Chr(&H1A)
ErrCode = 0
strContents = FileContentsAsString(FileSpec, False, ErrCode)
If ErrCode = 0 Then
'File has been read
If Right(strContents, Len(EOF)) = EOF Then
'EOF found: remove it
strContents = Left(strContents, Len(strContents) - Len(EOF))
End If
ErrCode = WriteToFile(strContents, FileSpec, True)
End If
StripEOF = (ErrCode = 0)

End Function




Function FileContentsAsString(FileSpec As Variant, _
Optional ReturnErrors As Boolean = False, _
Optional ByRef ErrCode As Long) As Variant
'By John Nurick 2003-05
'Retrieves contents of file as a string
'Silently returns Null on error unless
' ReturnErrors is true, in which case
' uses CVErr() to return an error value.
' Optionally, you can retrieve the error
' code in the ErrCode argument

Dim lngFN As Long

On Error GoTo Err_FileContents
If IsNull(FileSpec) Then
FileContentsAsString = Null
Else
lngFN = FreeFile()
Open FileSpec For Input As #lngFN
FileContentsAsString = Input(LOF(lngFN), #lngFN)
Close #lngFN
End If
ErrCode = 0
GoTo Exit_FileContents

Err_FileContents:
ErrCode = Err.Number
If ReturnErrors Then
FileContentsAsString = CVErr(Err.Number)
Else
FileContentsAsString = Null
End If
Err.Clear
Exit_FileContents:
Close #lngFN
End Function




Function WriteToFile(Var As Variant, _
FileSpec As String, _
Optional Overwrite As Long = True) _
As Long
'Writes Var to a textfile as a string.
'Returns 0 if successful, an errorcode if not.
'By John Nurick

'Overwrite argument controls what happens
'if the target file already exists:
' -1 or True (default): overwrite it.
' 0 or False: append to it
' Any other value: abort.

Dim lngFN As Long

On Error GoTo Err_WriteToFile
lngFN = FreeFile()
Select Case Overwrite
Case -1 'True
Open FileSpec For Output As #lngFN
Case 0 'False
Open FileSpec For Append As #lngFN
Case Else
If Len(Dir(FileSpec)) > 0 Then
Err.Raise 58 'File already exists
Else
Open FileSpec For Output As #lngFN
End If
End Select
Print #lngFN, CStr(Nz(Var, ""));
Close #lngFN
WriteToFile = 0
Exit Function
Err_WriteToFile:
WriteToFile = Err.Number
End Function
 
Back
Top