Solved

sscanf function in VB?

Posted on 1998-05-18
8
1,765 Views
Last Modified: 2011-03-21
Is there a function in VB that is simmilar to the C function sscanf?
0
Comment
Question by:hubermar
[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
  • 5
  • 2
8 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 50 total points
ID: 1452832
There is no directly corresponding function but you can manipulate a string and convert text to numbers. If your string was of the form "FRED 1234 XYZ". you could write a sub such as

sub(text1 as string,numeric1 as double,text2 as string,target as string)
dim i as integer
dim length as integer
dim text3 as string

i = 1
length = len(target)

do until i > length or target(i) = " "
    text1 = text1 + target(i)
loop

i = i + 1

do until i > length or target(i) = " "
    text3 = text3 + target(i)
loop
numeric1 = val(text3)

i = i+1
     
do until i > length or target(i) = " "
    text2 = text2 + target(i)
loop
 
end sub
     
You'd have to tailor individual functions for individual circumstances - You could probably use variants for the variables and then select appropriate translation by 'chopping up'  a format string in the form "%d %s %d" etc

0
 
LVL 5

Expert Comment

by:bin_huwairib
ID: 1452833
No huberma, there is no function in VB is similar to sscanf, and I didn't find any API function equivalent to it, but you can use the normal string manipulation functions instead (like InStr, Mid, Left...).

Bin Huwairib
0
 
LVL 18

Expert Comment

by:deighton
ID: 1452834
CORRECTION TO STRING MANIPULATION

    sub(text1 as string,numeric1 as double,text2 as string,target as string)
    dim i as integer
    dim length as integer
    dim text3 as string

    i = 1
    length = len(target)

    do until i > length or mid(target,i,1) = " "
        text1 = text1 + mid(target,i,1)
    loop

    i = i + 1

    do until i > length or mid(target,i,1) = " "
        text3 = text3 + mid(target,i,1)
    loop
    numeric1 = val(text3)

    i = i+1
         
    do until i > length or mid(target,i,1) = " "
        text2 = text2 + mid(target,i,1)
    loop
     
    end sub
0
Technology Partners: 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!

 

Author Comment

by:hubermar
ID: 1452835
deighton,

That's the method that I'm currently using.  It's a real pain.  I appreciate your suggestion.

Thanks,
Mark
0
 
LVL 18

Expert Comment

by:deighton
ID: 1452836
What are you trying to do exactly?  I can think of ways of improving my function.
0
 
LVL 18

Expert Comment

by:deighton
ID: 1452837
'Here's a more complcated example - hope it helps


Sub sscanf(ByVal sBuff As String, ByVal sFormat As String, Optional a, Optional b, Optional c, Optional d)

'Attempt to bring Sscanf function into VB
'uses optional variant parameters
'MAX four parameters in scanf string - but this COULD BE INCREASED

'EXAMPLE sscanf("1234 1.2 6","%i %d %i",a,b,c)
' puts 1234 in a (a is integer)
'        1.2 in b (d is a double)
'       6   in c (c is an integer)

Dim sTemp As String
Dim sID As String
Dim i As Integer
Dim i2 As Integer

'Extract paramater a

Call strip(sBuff, i, sTemp)
Call strip(sFormat, i2, sID)
Call value(sTemp, sID, a)

If IsMissing(b) Then Exit Sub

sBuff = Right(sBuff, Len(sBuff) - i - 1)
sFormat = Right(sFormat, Len(sFormat) - i2 - 1)

'extract parameter b

Call strip(sBuff, i, sTemp)
Call strip(sFormat, i2, sID)
Call value(sTemp, sID, b)

If IsMissing(c) Then Exit Sub

'You would repeat the sections to add new parameters e,f,g,... and put them in the parameter list


sBuff = Right(sBuff, Len(sBuff) - i - 1)
sFormat = Right(sFormat, Len(sFormat) - i2 - 1)

'extract parameter c

Call strip(sBuff, i, sTemp)
Call strip(sFormat, i2, sID)
Call value(sTemp, sID, c)

If IsMissing(d) Then Exit Sub

sBuff = Right(sBuff, Len(sBuff) - i - 1)
sID = Right(sFormat, Len(sFormat) - i2 - 1)

Call strip(sBuff, i, sTemp)
Call strip(sFormat, i2, sID)
Call value(sTemp, sID, d)

End Sub

Sub strip(sBuff As String, iReturn As Integer, sStrip As String)
    'Strip the next value or control code out of the string

    Dim i As Integer

    i = 1
    Do Until i > Len(sBuff) Or Mid(sBuff, i, 1) = " "
        i = i + 1
    Loop
   
    sStrip = Left(sBuff, i - 1)
    iReturn = i - 1

End Sub


Sub value(sExtract As String, sFormat As String, vResult)
   
    If sFormat = "%i" Then
        vResult = Int(Val(sExtract))
    ElseIf sFormat = "%s" Then
        vResult = sExtract
    ElseIf sFormat = "%d" Then
        vResult = Val(sExtract)
    End If
 
End Sub


'EXAMPLE

Private Sub Command1_Click()

    'example function call

    Dim i As Integer
    Dim s As String
    Dim d As Single
    Dim d2 As Double
    Call sscanf("1234 FRED 1592654 1", "%i %s %d %d", i, s, d, d2)
    MsgBox i
    MsgBox s
    MsgBox d
    MsgBox d2
End Sub

0
 

Author Comment

by:hubermar
ID: 1452838
deighton,

Very impressive.  I thank you for your help.  I will include this in my code.  I wasn't expecting so much help.  You went the extra mile to answer my question.  

Is it permissible to notify you when I ask questions in the future?  

Thanks again,
Mark
0
 
LVL 18

Expert Comment

by:deighton
ID: 1452839
E-Mail

DEIGHA@GUSCO.COM
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…

733 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