Solved

Read Ascii string from file

Posted on 2004-08-23
5
277 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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

839 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