Solved

Read Ascii string from file

Posted on 2004-08-23
5
264 Views
Last Modified: 2012-06-27
Hi all,
I have a template for a certain type of file that i'm trying to read. Basically the file is should have a header of 80 bytes that are ASCII text giving the files name. After that its a series of 2 byte numbers which I can read fine.
however when I try and extract the name from the file it never works, the string is always nothing. Here is my code

Dim nFileNum As Integer = FreeFile()
            FileOpen(nFileNum, File, OpenMode.Binary, OpenAccess.Read, OpenShare.LockReadWrite)
           
            Dim FileName As String
            FileGet(nFileNum, FileName, 1, False)

            If Not FileName Is Nothing Then
                Me.ComboBox_Models.Items.Add(Convert.ToString(FileName))
            Else
                Me.ComboBox_Models.Items.Add("Model #" & (Me.MeshIndex + 1))
            End If


Please ignore the fact that I'm trying to convert a string into a string when addin the combo box. This is there only becuase I have tried changing FileName to different variable types like byte()

Here is a link to the file type:
http://www.ennex.com/~fabbers/StL.asp

its right at the bottom.

Any help would be much appreciated.

Cheers
DRB2k2
0
Comment
Question by:drb2k2
  • 2
  • 2
5 Comments
 
LVL 7

Expert Comment

by:Burbble
ID: 11871043
I'm not sure of the code in your FileGet() function, but this should work for you:

Open File For Input As #nFileNum
    FileName = Input(80, #nFileNum)
Close #nFileNum

-Burbble
0
 
LVL 3

Accepted Solution

by:
PocketLintPPC earned 50 total points
ID: 11871082
When reading stand-alone strings, prefill the sting to the size of the data you want to read and use the GET statement. The variable should be a string not a variant as reading into a variant woudl require the file to have extra data to tell the variant the data type and data size. The link you provided does to the file layout does not support reading into variants.

Dim FileName as string

Filename = space(80)
Get #nFileNum, , FileName

Will read the fist 80 bytes into a string.
to test for an empty string, using TRIM(FileName) would work better than testing for nothing as strings can not be set to nothing.


hope this helps some
0
 
LVL 3

Expert Comment

by:PocketLintPPC
ID: 11871168
Wish I could edit my posts...

Also using GET allows you to read any variable type, so reading into a long will read 4 bytes so..

dim LongVar as long
Get #nFileNum, , LongVar

Get looks at the size of the variable to be read into and uses that size to fetch the data (with some exceptions, see the get statement in the VB helps file for more info)
0
 
LVL 7

Expert Comment

by:Burbble
ID: 11871191
^--- His way is better :-)

I didn't realize that you could do that. Binary methods are generally faster than the Input/Output methods, so it's usually better to use them.

-Burbble
0
 

Author Comment

by:drb2k2
ID: 11879070
Thank you very much PocketLintPPc that works like a charm
Also many thanks to Burrble for ansering with a very decent suggestion.
CHeers both
Drb2k2
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now