Solved

Read Ascii string from file

Posted on 2004-08-23
5
250 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now