CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2000
    Posts
    440

    reading form text file

    Hi,

    I need to read a line from text file. I tried to use input statement, however it reads it untill separator. i.e if I have a comma in htis line it reads until it and the next read continues after it. So how can I read the whole line to the end?

    Thanks

    Valery Iskarov Nikolov
    http://listen.to/quark

  2. #2
    Join Date
    Jun 2001
    Location
    Memphis, TN
    Posts
    146

    Re: reading form text file

    See if this code helps out.. it is based on the Windows Script Host object FileSystemObject.


    Dim fso
    Dim myfile
    set fso = CreateObject("scripting.filesystemobject") 'create the FileSystemObject
    set myfile = fso.opentextfile("c:\test.txt") 'use path to your text file

    sLine = Trim(myfile.readline) 'read the first line
    MsgBox sLine 'show me the money





    To read multiple lines, just put the code with the ReadLine method in a loop and your file should be read sequentially.

    I just tested it to make sure that it will read through commas and it worked just fine. Hope that helps!


  3. #3
    Join Date
    Feb 2000
    Posts
    440

    Re: reading form text file

    Thank you very much.

    Regards

    Valery Iskarov Nikolov
    http://listen.to/quark

  4. #4
    Join Date
    Jun 2001
    Location
    Memphis, TN
    Posts
    146

    Re: reading form text file

    I haven't tested this code to make sure it runs through commas, but i think it should... this is if you prefer using the...


    Open "File" for input as #1




    ...method of file reading, this code should do the trick.


    Open "c:\test.txt" for input as #1 'open it up
    Line input #1, txt 'read the first line
    MsgBox txt 'show me some more of the money





  5. #5
    Join Date
    Feb 2000
    Posts
    440

    Re: reading form text file

    That was the first I tried but it wouldnt read through commas. Input statement treats commas like separator...

    I think file system object will do it.


    Valery Iskarov Nikolov
    http://listen.to/quark

  6. #6
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: reading form text file

    line input #1, oneLine

    where oneLine is a string variable.


  7. #7
    Join Date
    Jun 2001
    Location
    Memphis, TN
    Posts
    146

    Re: reading form text file

    Were you using "Input" or "Line Input" to read the lines before? Line Input reads through to the line feed.


  8. #8
    Join Date
    Feb 2000
    Posts
    440

    Re: reading form text file

    Thanks, Line Input # Statement did the job perfectly.

    Regards

    Valery Iskarov Nikolov
    http://listen.to/quark

  9. #9
    Join Date
    Feb 2000
    Posts
    440

    Re: reading form text file

    thanks,

    I did'n notice the difference between "line input" and "input" statements

    regards,

    Valery Iskarov Nikolov
    http://listen.to/quark

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