Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

VB File Handling....

  • 09-04-2002 04:16PM
    #1
    Closed Accounts Posts: 7,230 ✭✭✭


    Ok lets say i have five variables of type String. I need to be able to put the values that these variables hold into a file and was wondering how do i do this? What i do is i ask the user 5 questions, each answer is assigned to a variable, and then printed to a file. please help!!! Thanks in advance!@#


Comments

  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Here's a quick sub with sVar1 to sVar5 as your variables and sLogFileName as the text file you want to save them to (in a CSV format).
    Public Sub logVars(sVar1 As String, sVar2 As String, sVar3 As String, _
        sVar4 As String, sVar5 As String, sLogFileName As String)
        
        Dim Temp As String
        Dim i As Integer
        
        i = FreeFile
        Temp = """" & sVar1 & """,""" & sVar2 & """,""" & sVar3 & ""","""
        Temp = Temp & sVar4 & """,""" & sVar5 & """"
            
        Open sLogFileName For Append As #i
            Print #i, Temp
        Close #i
    End Sub
    


  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    thanks!


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Looks a bit messy. Wouldn't this be better?
    
    Public Sub logVars(sVar() As String, sLogFileName As String)
    
        total = ubound(sVar) 
    
        QUOTE = chr$(34)
    
        ff = freefile
        open sLogFileName for append as #ff
    
          for n = 1 to total
               print #ff, QUOTE;sVar(n); QUOTE;
               if n < total then 
                     print #ff, ","
               else
                     print #ff, ""
               endif
          next n
    
       close #ff
    
    end sub
    

    Although I'd do

    Public Sub logVars(sVar() As String, fileHandle As Integer)

    and open up the file and call the sub to write the record.


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Hmmpfh... Show off... At least I declared my variables ;)

    (btw, just noticed the else in your code is superfluous)


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Originally posted by The Corinthian
    Hmmpfh... Show off... At least I declared my variables ;)

    (btw, just noticed the else in your code is superfluous)

    Yes Bold Hobbes should declare variables! I even put an "option explicit" normally to force me to.

    As for the ELSE, it's needed but there is a bug there. It should be...
               if n < total then 
                     print #ff, ","[b];[/b]
               else
                     print #ff, ""
               endif
    

    This tells it to add a comma to the line except at the last variable where it will print a 0x0d0a (CRLF)


  • Advertisement
  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Yes, I see. Still I'd probably not bother with the ELSE and just put the print #ff, "" just after closing the FOR/NEXT loop:
    for n = 1 to total
        print #ff, QUOTE;sVar(n); QUOTE;
        if n < total then print #ff, ","
    next n
    print #ff, "" 
    
    Better still, wouldn't a FOR/EACH be better than using FOR/NEXT?

    101 Ways to skin a cat, I expect...


  • Registered Users, Registered Users 2 Posts: 16,415 ✭✭✭✭Trojan


    Geez lads, get a room ffs :)

    Al.


Advertisement