Open statements Read and write to text and binary files

Source: Internet
Author: User

Operation of a text file
This method is the basic unit of the Unit of Behavior reading, the main application of methods and functions haveOpen,close,line Input,freefile,Eofsuch as Briefly describe its capabilities and then combine the code examples to illustrate them.
Open: As the name implies, its role is to open a file, in other words, to open a file is to gain some control, in general, when the file is open, only the opener can manipulate it. When you open a file, you specify an integer as the file number, and subsequent operations are done for that code, not for the file name. The file number is also called a handle, in the program a file number can only point to a file, cannot appear two files with the same handle case.
Close:Close the file, that is, release control of the file.
Line Input:Gets the contents of the file in the behavior unit, which is the content from the current position to the next line break. Line break isCHR & Chr (10)Consisting of two bytes,VbA constant has been defined inVbCrLfAnd can be used directly. It's important to note thatLine InputWhen you read a line, you remove the line breaks at the end of the row, so we remember to fill in line breaks to keep the content consistent with the file as we read the contents of each row.
FreeFile:Get the free file number, use this function to get the file number can avoid the conflict of file number.
For example:
Dim strFileName as String 'Filename
Dim Lnghandle as Long 'File handle
Dim Strall as String 'All contents of the text file being read
Dim StrLine as String 'Store the contents of each row in a loop
strFileName = "C:\b.txt"
‘Get a handle to a file
Lnghandle = FreeFile ()
' ForThe arguments that follow indicate how the file is opened,Inputis read,Outputis overwrite write,Appendis an append write
Open strFileName for Input as Lnghandle
‘Loop until end of file
Do and not EOF (Lnghandle)
‘Each read line is stored in theStrLineVariables in
Line Input #lngHandle, StrLine
‘Each read connects the read content to theStrallVariable, becauseLine InputThe line break is removed, so here's the
Strall = Strall & StrLine & VbCrLf
Loop
‘Show the full part of the content
MsgBox Strall, vbinformation
Writing a text file is relatively straightforward, with three steps: Open the file, write the text, and close the file.
code example:
Dim strFileName as String 'Filename
Dim Lnghandle as Long 'Handle
Dim Strwrite as String 'The text content to write
strFileName = "C:\w.txt"
Lnghandle = FreeFile () 'Get handle
‘Prepare the content to be written
Strwrite = "The head of the Sunset Building, the voice of the hung, Jiangnan wandering. The Wu hook to see, the column dry pat all over, no one will, boarding meaning."
Open strFileName for Output as Lnghandle 'Open File
Print #lngHandle, Strwrite 'Writing text
Close Lnghandle 'Close File
MsgBox "Write complete.", vbinformation
Operation of binary files
The storage of all files is essentially binary, and binary files are often made up of two parts, part of which is the contents of the file in the other part of the file. The file header usually holds information about the file format toBmpsuch as image files, their file header contains information such as graphics format, image size, color palette, to display the image when the first read the file header to obtain the details of the file, and then in its format to display the image content. In binary mode, you can manipulate any file, but it is of course important to understand the file header structure of the file you are manipulating.
VbThe main methods and functions used in binary file operations are:Open,close,get,putsuch as
Open: InForThe following open mode is usedBinary。
Close:Close the file.
Get:Gets the contents of the file at the specified location and reads from the current location if the location is omitted.
Put:Writes the file at the specified location, if the position parameter is omitted from the current position.
The following code explains the operation of binary files.
The following procedure completes the combination of two files into a file and the combined file is split into the original two files. The structure of the file header is defined by itself, very simple, a total8Bytes (that is, the length of the two long integers), the first4Bytes to hold the length of the first file, followed by a4Bytes to hold the length of the second file. In order to express the structure of the file more visually, we now assume that there are two files, the first file length is100Bytes, the second file is200Bytes, then the resulting file should be308Bytes. In order, the structure of the file is:
4The length of the first file
4The length of the second file
100Binary content of the first file
200Binary content of the second file
In addition to the file header8The length of the byte is fixed and the subsequent length varies depending on the file.
‘Implementing a merge of files

Private Sub Mergefile ()

Dim strFileName1 as String 'First file

Dim strFileName2 as String 'A second file

Dim Stroutput as String 'The merged file

Dim arycontent () as Byte 'An array to read the file to redefine before each read



strFileName1 = "C:\a.bmp"

strFileName2 = "C:\b.bmp"

Stroutput = "C:\out.bmp"



‘Note the following threeOpenStatements are all using theBinaryMode to open the

Open stroutput for Binary as #100

Open strFileName1 for Binary as #1

Open strFileName2 for Binary as #2

Put #100, LOF (1) 'Gets the length of the first file and writes it to the file header of the merged file

Put #100, LOF (2) 'Gets the length of the second file and writes to the file header of the merged file

ReDim arycontent (LOF (1)-1) 'Redefine array, prepare to read files

Get #1, arycontent () 'Gets the contents of the first file to the array

Put #100, arycontent () 'Write the contents of the first file to the merged file

ReDim arycontent (LOF (2)-1)

Get #2, Arycontent ()

Put #100, Arycontent ()

‘Close File

Close #1

Close #2

Close #100

End Sub

‘Implementing a split of a file

Private Sub Splitfile ()

Dim strFileName1 as String

Dim StrFileName2 as String

Dim Strfilesplit as String

Dim arycontent () as Byte

Dim Lnglof (1) as Long 'Store the length of the two files obtained from the file header

strFileName1 = "C:\a2.bmp"

strFileName2 = "C:\b2.bmp"

Strfilesplit = "C:\out.bmp"

Open strfilesplit for Binary as #100

Get #100, Lnglof (0) 'Get the length of the first file

Get #100, Lnglof (1) 'The length of the second file

Open strFileName1 for Binary as #1

Open strFileName2 for Binary as #2

ReDim arycontent (Lnglof (0)-1) 'Redefine the array with the length of the first file to prepare for reading the first file

Get #100, 9, arycontent () 'From the first9
   put #1, arycontent () ' write the obtained content to the split file

   ReDim arycontent (Lnglof (1)-1)

   Get #100, 9 + lnglof (0) + 1, arycontent () ' from 9

   Put # 2, arycontent

     close #1

   close #2

   close #100 & nbsp; 

   MsgBox " split complete.

Open statement reads and writes text and binary files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.