CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2000
    Location
    canada
    Posts
    124

    Send data to file

    Hi!!!

    I need to know how to send data to a specific file. Not a database, just a text file.

    Someone can help me with that?!

    Isabelle


  2. #2
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: Send data to file

    There are two ways of doing this:
    1) Works with native VB code, but not as flexible:

    Sub WriteToFile(sTextToWrite as string)
    Dim i as Integer
    Dim fn as string

    fn = App.Path & "\testing.txt"

    i = FreeFile

    Open fn for Output as i

    print #i, sTextToWrite

    Close i
    End Sub




    Works, but not very interesting.

    2)Set a reference to the Microsoft Scripting Runtime

    Sub WriteToFile(sTextToWrite as string)
    Dim FSO as Scripting.FileSystemObject
    Dim objWriter as Scripting.TextStream
    Dim fn as string

    set FSO = new Scripting.FileSystemObject

    fn = App.Path & "\testing2.txt"
    'the filename, how to open the file, can we create this file?
    set objWriter = FSO.OpenTextFile(fn,ForWriting,true)

    objWriter.Write sTextToWrite 'writes only the text supplied
    objWriter.WriteLine sTextToWrite 'writes the text and places a vbCrLf at the end

    objWriter.Close

    set objWriter = nothing
    set FSO = nothing
    End Sub



    I personally like the second way, because it's easier to append to an existing file, or just create the new file.

    Good luck,
    john

    John Pirkey
    MCSD
    www.ShallowWaterSystems.com
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

  3. #3
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Send data to file

    You can write data on several ways to a file. First you need to open a file
    open "pathToFile" for input/output/append/binary as #filenum


    Explanation:

    "PathToFile" is the path to the file, when a full path is not specified, it is the file in the same directory as the program. Note that when developing, this is the directory where the vb.exe is located.

    input: opens the file for read operations
    output: opens the file and clears it. Used for output operations.
    append: opens the file and appends the output at the end of the file. This creates a new file if the specified file does not exist.
    binary: opens a file for binary access

    #filenum: an integer value of a file used to refer to. Assign this value using the FreeFile statement.

    eg.
    myFileNum = FreeFile
    open "myFile.dat" for input as #myFileNum



    So, now the file is open, we can write to it. We can do this using the write or print command.

    strName = "Isabelle"
    print #myFileNum, "Name:" & strName
    ' this sends one line of data to a file, placing it on a new line
    Write #myFileNum, "Name",strName



    Now we have written what we had to write, we must close the file using the close command.
    Close #myFileNum



    The file we just written to should look like this:
    Name:Isabelle
    "Name","Isabelle"

    The first line was written using the Print command, the second using the write command.
    Now we may just want to retrieve that data we have written. This we do using the Line Input or Input. We open the file for input. and read from the file.

    myFileNum = FreeFile
    Open "myFile.dat" for input as #myFileNum
    Line input #myFileNum, strLine 'reads a line from the file and places it in strData
    input #myFileNum, strField, strValue
    Close #myFileNum



    Note that using the Input command, we specify 2 strings to get the values. This is because the line we read is "Name","Isabelle". "Name" will be assinged to strField, "Isabelle" will be assigned to strValue.


    Tom Cannaerts
    [email protected]

    The best way to escape a problem, is to solve it.
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  4. #4
    Join Date
    Sep 1999
    Location
    Germany
    Posts
    66

    Send data to file in one call

    This is the simple method to save a file from a textbox in one call.


    Dim fnumber as Integer
    Dim fname as string

    fnumber = FREEFILE
    fname = "demo.txt"

    Open fname for Output as #fnumber

    print #fnumber, (Text1)

    Close #fnumber




    Please rate the solutions so that people feel happy and encourage to reply and answer your questions.

    [email protected]
    http://vbcode.webhostme.com/

  5. #5
    Join Date
    Feb 2000
    Location
    garden grove, california
    Posts
    64

    Re: Send data to file

    these idiots that replied to you said to much bs. You just want the simple stuff right? here it is, this is just saving the text in a textbox to a simple text file:

    open "c:\whatever.txt" for ouput as #1
    input #1, text1
    close #1


  6. #6
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: Send data to file

    Oh! Heaven forbid we offer options and explanations.

    John Pirkey
    MCSD
    www.ShallowWaterSystems.com
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

  7. #7
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Send data to file

    >these idiots that replied to you said to much bs. You just want the
    >simple stuff right? here it is, this is just saving the text in a textbox
    >to a simple text file:

    Oh panasonic / starcraft / The Matrix / Whatever you call yourself today - you have such a way with words, and can be so pleasant to the rest of the forum members.

    All of the posts that replied to the original are far better than yours and for one reason - yours doesn't work.

    >open "c:\whatever.txt" for ouput as #1
    >input #1, text1
    >close #1

    So you're trying to convince us that the 'Input' statement in VB can be used to *write* data to a file ?

    Plus the fact that you are using #1 as a file handle, when any programmer whos ever read a VB manual / helpfile will tell you to get a filehandle from the 'FreeFile' function first.

    May I suggest that you think first, try out your code (and other peoples) responses before jumping in and flaming the other members of this group ?


    Chris Eastwood

    CodeGuru - the website for developers
    http://codeguru.developer.com/vb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured