473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,396 developers and data experts.

How to read a file in VB - Part 2 - VB6, Binary mode (Get)

8,435 Expert 8TB
Here is a very simple example routine which reads a file from disk, in one big lump. This uses the built-in VB statements only. Later we will cover the FileSystemObject, which provides greater functionality at the expense of slightly greater code complexity.

This self-contained routine can be pasted into a code module and called from anywhere, including the immediate window. It will expect you to pass the name of a file (including the path if it isn't in the current directory) and will copy the contents of the file to the immediate window. Note it will also avoid, as far as possible, interfering with any other processing which may be going on at the time.

The extra parameter (compared to the prior sample in this series) allows you to request that the file contents be dumped as one big string (appropriate for a text file) or byte by byte (appropriate for most other file types).

Expand|Select|Wrap|Line Numbers
  1. Public Sub DumpFile_V02(ByVal FileName As String, ByVal TreatAsText As Boolean)
  2.   Dim FileNo As Long
  3.   Dim FileSize As Long
  4.   Dim Buffer As String
  5.   Dim CharNo As Long, Char As String * 1
  6.  
  7.   FileNo = FreeFile ' Get next available file number.
  8.   Open FileName For Binary Access Read Shared As #FileNo
  9.   FileSize = LOF(FileNo) ' Determine how large the file is (in bytes).
  10.   Buffer = Space$(FileSize) ' Set our buffer (string) to that length.
  11.  
  12.   ' The length of the string (Buffer) determines how many bytes are read...
  13.   Get #FileNo, , Buffer ' Grab a chunk of data from the file.
  14.   Close #FileNo
  15.  
  16.   ' Display the results, either as one big chunk or byte-by-byte.
  17.   If TreatAsText Then
  18.     Debug.Print Buffer
  19.   Else
  20.     For CharNo = 1 To FileSize
  21.       Char = Mid(Buffer, CharNo, 1)
  22.       Debug.Print Format(CharNo, "#,###"); " = Decimal(" _
  23.           ; Format(Asc(Char), "000"); ")  Hexadecimal(" _
  24.           ; Hex$(Asc(Char)); ")  ["; Char; "]"
  25.       DoEvents
  26.     Next
  27.     Beep ' Just let the user know we're finished.
  28.   End If
  29. End Sub
May 18 '07 #1
0 20500

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Guyon Morée | last post by:
what is the difference? if I open a text file in binary (rb) mode, it doesn't matter... the read() output is the same.
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
4
by: sree | last post by:
i am doing project a simple http server. it is being writen in c using sockets. so when there is a request to read a jpeg or any other file icant do it. my code is working only for html and txt...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
9
by: sweety | last post by:
Dear All, How to encrypt a C data file and make binary file and then have to read a bin file at run time and decrypt the file and have to read the data. Any help to achive this pls. Would be...
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
7
by: Hallvard B Furuseth | last post by:
I'm trying to clean up a program which does arithmetic on text file positions, and also reads text files in binary mode. I can't easily get rid of it all, so I'm wondering which of the following...
3
by: utab | last post by:
Dear all, What are the advantages of binary files over text files? I would like to search for a specific value of a variable in an output file, I was doing this lately by the string library...
4
by: Matrixinline | last post by:
Hi All Here is my problem I am using a Unicode project and I tried to read the File like sPath = LPCTSTR; FILE* oFp = _tfopen(sPath,L"r"); while(!feof(oFp))
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.