CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2001
    Posts
    24

    A few questions about text files.

    1.) When displaying text from a text file in a text box, is there a way to display it all just how it is, without knowing if there are any commas or line breaks? I have been using the "Open file$ for Input as" method, but If I don't know how many lines there are, or if there are commas, then I have a high chance of getting the past end of input error.

    2.) If there is a folder full of text files, is there any way to open a random one without knowing the name of the file?

    Thanks in advance


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

    Re: A few questions about text files.

    1) How are you getting past the end of file? Aren't you using the While Not EOF(filenum) syntax?

    2) If you use the FileSystemObject, the Folder object has a Files collection - pass it a random number between 1 and the Folder.Files.Count value to open a random file.

    have fun,

    john


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

  3. #3
    Join Date
    May 2001
    Location
    Canada
    Posts
    7

    Re: A few questions about text files.

    1)
    Open file$ For Input As #x
    Do Until EOF(x)
    'Your reading in code here.
    Loop
    Close #x

    Note: Might consider using Line Input #x instead of just Input #x depending on the situation.

    2) Place a FileList control on your page. Make it invisible. Change the FileList.Path to whatever the path is. Select a random number from 0 to the .ListCount-1 {RandomFile = int(rnd*(FileList.ListCount-1))}. Your random file name is FileList.List(RandomFile).


  4. #4
    Join Date
    Apr 2000
    Posts
    737

    Re: A few questions about text files.

    refer http://vblib.virtualave.net

    1.) ReadFileText in vbFileIO which read a file into string array and return the no. of line for the file.

    2) GetAllFileName in vbFileIO return all filename in a folder and return the no of files in that folder, then you can just use the VB function Randomize & Rnd

    HTH


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

    Re: A few questions about text files.

    The following routine willread a text file into a text box leaving all commas, line feeds, Carriage returns, etc intact.

    ' Read a file into a TEXT box using binary read
    ' this reduces the exposure of VB editing double quote marks and
    ' commas out of the text

    private Sub Command1_Click()
    Dim a
    a = "C:\to DO LIST\1.txt"
    Open a for binary as #1
    Text1.Text = input(FileLen(a), #1)
    Close

    End Sub





    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