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

    how to read text on text file and store to variable

    I need to read a text from a text file and store it to variable and use it as a parameter to pass

    rgds
    cyrus

  2. #2
    Join Date
    Jul 2008
    Location
    Appleton, WI
    Posts
    23

    Re: how to read text on text file and store to variable

    This can be done by creating an instance of Scripting.FileSystemObject and using the methods that this object provides. I am attaching a link to the msdn library site for the FileSystemObject. This should point you in the right direction.

    http://msdn.microsoft.com/en-us/libr...06(VS.60).aspx

    Assigning to a variable and passing the value or reference as a parameter should be easy once you start using the FileSystemObject.

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: how to read text on text file and store to variable

    It can be done several different ways. I generally do not use the scripting object. I prefer to use the standard i/o methods when they do the job. Using the standard functions you can read an item, a line, a record [in a fixed length file], x number of bytes starting at y position and of course the entire file.

    Are you trying to read a line, a variable or the whole file?

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: how to read text on text file and store to variable

    To open a text file and read up to the first crlf [e.g. the first line of text] you would use something like.
    Code:
    Dim LineFromFile as String
    Dim FileHandle as Integer
    
    FileHandle=FreeFile
    
    Open "C:\MyFile.Txt" for input as #FileHandle
    Line Input #FileHandle,LineFromFile
    Close #FileHandle

  5. #5
    Join Date
    Jul 2008
    Location
    Appleton, WI
    Posts
    23

    Re: how to read text on text file and store to variable

    DataMiser - Have to agree the standard I/O are nice. I use them for binary/random file access. I just prefer to use the filesystemobject for text files since it provides nice ways of verifying the folder/file exists without getting into error handling routines.

    I also find working with the filesystemobject makes my code more readable. But that my just be my experience since I have spent more time with text files then binary/random.

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: how to read text on text file and store to variable

    I've been working with Basic since the C64 days and those standard i/o routines have always been there. To me it is the way to go in most cases. The scripting object I have used in web pages and embedded devices where the standard i/o was not available but imo the scripting object is very limiting and has a bit of unneeded overhead,

  7. #7
    Join Date
    Jul 2008
    Location
    Appleton, WI
    Posts
    23

    Re: how to read text on text file and store to variable

    That is it right there. I am a self taught programmer with about 2 yrs experience. I started with 6.0, so I've always had the object.

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