473,383 Members | 1,929 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,383 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 20493

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))
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...
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...

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.