?
Solved

Read Ascii string from file

Posted on 2004-08-23
5
Medium Priority
?
302 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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

850 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