Solved

Read Ascii string from file

Posted on 2004-08-23
5
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS Access Search and Replace Using VBA 6 92
Run code from text file in vb 1 87
How does CurrentUser work? 10 42
Excel Automation VBA 19 104
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…
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…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

726 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