CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: File Reading

  1. #1
    Join Date
    Aug 2000
    Location
    South Africa
    Posts
    195

    File Reading

    Me again
    I receive a downloaded file from a mainframe env. that I process. How do I input this info if the file is comma delimited? How do I tell VB that the file is delimited? Data looks like this:
    "i",1,"017925","2001/03/31","0380248.00"

    Pse help!

    thanks




  2. #2
    Join Date
    Aug 2000
    Location
    England
    Posts
    185

    Re: File Reading

    Use the INSTR command to determine the position of the commas in the string and split it that way.


  3. #3
    Join Date
    Aug 2000
    Location
    South Africa
    Posts
    195

    Re: File Reading

    ok thanks - I've looked up the format of the INSTR command, I'm not sure how to do it. If I find a comma in the input string - how do I handle it code wise? Obviously I'll have to have some sort of a counter that I can substract 1 from if the current char is a comma?



  4. #4
    Join Date
    Aug 2000
    Location
    England
    Posts
    185

    Re: File Reading

    The instr command will return the first position in which the comma is found. Therefore, if you have more than one comma then you will need to set the starting position for the search to be higher than the position of the previous comma, otherwise it will keep finding the same one, i.e



    commaPos = 1

    Do

    previous = commaPos

    ' find the first - or next comma position

    commaPos = instr(myText, start, ',')

    ' if it cannot be found then exit

    if (commaPos = 0) then
    exit do
    end if

    ' DO SOMETHING HERE WITH THE DATA

    ' set the start position so that it looks for
    ' the next comma

    start = commaPos + 1
    Loop





    In order to extract the chunk of text use the Mid command with the variables start and previous to give you the relavent positions.

    Hope this helps

    Andrew


  5. #5
    Join Date
    Aug 1999
    Posts
    33

    Re: File Reading

    Hi,
    this is a v. simple eg. pl. modify it
    dim arr() as string
    arr = split("qwe,asd,zxc", ",")
    arr(0) = qwe
    arr(1) = asd
    arr(2) = zxc
    also ubound(arr) + 1 = total no of values that u wanted.
    Hope this helped.
    ~A.


  6. #6
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: File Reading

    I believe you should be able to use the INPUT statement to read this file. It can handle comma delimited files. Here is an example program that writes some data to a file. The data ends up looking just like your sample. The program then reads this file back using the INPUT statement.
    '
    Start a new project, add two command buttons. Paste this code into the general declarations section of the form.
    Change the strMyString variable in the Form_Load event to point to a valid disk location.
    Run the program.
    Click Command1 to create the file.
    Use notepad to look at the newly created file.
    Click Command2 to read the file back.

    option Explicit
    Dim strMyFile as string
    Dim ffile
    private Sub Command1_Click()
    ffile = FreeFile()
    Open strMyFile for Output as #ffile
    ' next statement writes comma delimited data.
    Write #ffile, "abd", "DEF", "ghi", "JKL"
    Close ffile
    End Sub
    '
    private Sub Command2_Click()
    Dim var1, var2, var3, var4
    ffile = FreeFile()
    Open strMyFile for input as #ffile
    input #ffile, var1, var2, var3, var4
    Close ffile
    Debug.print var1; " "; var2; " "; var3; " "; var4

    End Sub
    '
    private Sub Form_Load()
    strMyFile = "C:\VB Stuff\Things to look at\Junk.txt"
    End Sub




    John G

  7. #7
    Join Date
    Aug 2000
    Location
    South Africa
    Posts
    195

    Re: File Reading

    Very helpful thanks

    One question - what does Freefile() mean?




  8. #8
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: File Reading

    When you open file as #1 or #2 yoou might get an error because this number is already occupied by other process. FreeFile gives you the next available free number

    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  9. #9
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: File Reading

    FreeFile() is a VB function that will return the next available file Number for use in file management statements such as Open, Close, Read Write, Input, Line Input etc.
    It is documented in MSDN HELP along with examples.

    John G

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