Opening and Reading a TXT File

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 12844 Views - Last Post: 13 April 2007 - 10:09 AM Rate Topic: -----

#1 brottmayer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 21-January 07

Opening and Reading a TXT File

Posted 12 April 2007 - 11:17 AM

Hello,

I am using the below code and for some reason, it's not recognizing the code:

		Dim TextLine As String
		Dim Filename As String
		Filename = "c:\test.txt"
		Open (Filename) For Input as #1
		Line Input #1, TextLine
		TextBox1.Text = TextLine
		Line Input #1, TextLine
		TextBox2.Text = TextLine
		Close()



It would show a blue line underneath Open, For Input as #1, Line Input #1.

Basically, I have an external txt file that it'll read, test.txt. And in this file it has two lines of text, first line it says Test 1 and the second line it has Test 2. But it's not recognizing the command to open and to input. Please help! I have been working on this for over a week. Thanks in advance.

brottmayer

Is This A Good Question/Topic? 0
  • +

Replies To: Opening and Reading a TXT File

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Opening and Reading a TXT File

Posted 12 April 2007 - 01:23 PM

Remove the parenthesis from your Open statement.

Open Filename For Input as #1

Was This Post Helpful? 0
  • +
  • -

#3 DilutedImage  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 14
  • View blog
  • Posts: 646
  • Joined: 20-November 06

Re: Opening and Reading a TXT File

Posted 12 April 2007 - 03:00 PM

Though not your problem, you might want to consider using something like this, to accommodate multiple files:

Dim TextLine As String
Dim Filename As String
Dim FileNum As Integer
Filename = "c:\test.txt"
FileNum = FreeFile
Open Filename For Input as #FileNum
Line Input #FileNum, TextLine
TextBox1.Text = TextLine
Line Input #FileNum, TextLine
TextBox2.Text = TextLine
Close #FileNum



This post has been edited by DilutedImage: 12 April 2007 - 03:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 brottmayer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 21-January 07

Re: Opening and Reading a TXT File

Posted 12 April 2007 - 04:27 PM

View PostDilutedImage, on 12 Apr, 2007 - 03:00 PM, said:

Though not your problem, you might want to consider using something like this, to accommodate multiple files:


Jayman9, DilutedImage, I tried both of your suggestions, and this is the view that I am getting. Jayman9 your suggestion is the top and DilutedImage, yours on the bottom:

Posted Image

What do you think could be the cause of this "problem"? Thanks.

brottmayer
Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Opening and Reading a TXT File

Posted 12 April 2007 - 05:28 PM

What is the error message you are getting when you hover your cursor over the errors?
Was This Post Helpful? 0
  • +
  • -

#6 brottmayer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 21-January 07

Re: Opening and Reading a TXT File

Posted 12 April 2007 - 05:39 PM

View Postjayman9, on 12 Apr, 2007 - 05:28 PM, said:

What is the error message you are getting when you hover your cursor over the errors?


When i hover over the Open command i get: " 'Open' is not declared. File I/O functionality is available in the 'Microsoft.VisualBasic' namespace."

When I hover over Filename command i get: "Method arguments must be enclosed in parentheses"

When I hover over For command, i get: "Comma, ')', or a valid expression continuation expected."

When I hover over Line command i get: " 'Line' statements are no longer supported. File I/O functionality is available as 'Microsoft.VisualBasic.FileSystem.LineInput' and the graphics functionality is available as 'System.Drawing.Graphics.DrawLine'."

When I hover over Input command i get: "Overload resolution failed because no accessible 'Input' accepts this number of arguments."

When I hover over the # command i get: "Comma, ')', or a valid expression continuation expected."

Thanks for your help.

brottmayer
Was This Post Helpful? 0
  • +
  • -

#7 DilutedImage  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 14
  • View blog
  • Posts: 646
  • Joined: 20-November 06

Re: Opening and Reading a TXT File

Posted 12 April 2007 - 05:45 PM

Mine was meant as an alteration to the original code, rather than an addition. Just an enhancement. :) If you run the code with both mine and jayman9's, then you'll have problems with the variables being redeclared. Try just using jayman's code, and let us know if that works. If it does, then you can alter/replace it with what I showed, if you would benefit from handling multiple files simultaneously. Otherwise, just leave it as he has shown; that should work.

This post has been edited by DilutedImage: 12 April 2007 - 05:46 PM

Was This Post Helpful? 0
  • +
  • -

#8 brottmayer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 21-January 07

Re: Opening and Reading a TXT File

Posted 12 April 2007 - 05:45 PM

View PostDilutedImage, on 12 Apr, 2007 - 05:41 PM, said:

Mine was meant as an alteration to the original code, rather than an addition. Just an enhancement. :) If you run the code with both mine and jayman9's, then you'll have problems with the variables being redeclared. Try just using jayman's code, and let us know if that works. If it does, then you can alter/replace it with what I showed, if you would benefit from handling multiple files simultaneously. Otherwise, just leave it as he has shown; that should work.



Gotcha. Thanks DilutedImage. I'll let you know!

brottmayer
Was This Post Helpful? 0
  • +
  • -

#9 DilutedImage  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 14
  • View blog
  • Posts: 646
  • Joined: 20-November 06

Re: Opening and Reading a TXT File

Posted 12 April 2007 - 05:48 PM

I think I know what's going on ... What version of Visual Basic are you using?
Was This Post Helpful? 0
  • +
  • -

#10 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Opening and Reading a TXT File

Posted 12 April 2007 - 05:48 PM

I think I see the problem now. Are you working in VS2003 or VS2005?

You need to have an imports statement to use deprecated VB6 code.

Imports Microsoft.VisualBasic

Was This Post Helpful? 0
  • +
  • -

#11 brottmayer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 21-January 07

Re: Opening and Reading a TXT File

Posted 12 April 2007 - 05:51 PM

View Postjayman9, on 12 Apr, 2007 - 05:48 PM, said:

I think I see the problem now. Are you working in VS2003 or VS2005?

You need to have an imports statement to use deprecated VB6 code.

Imports Microsoft.VisualBasic



I am using Visual Basic 2005 Express Edition...Does that change something? Thanks.

brottmayer
Was This Post Helpful? 0
  • +
  • -

#12 DilutedImage  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 14
  • View blog
  • Posts: 646
  • Joined: 20-November 06

Re: Opening and Reading a TXT File

Posted 12 April 2007 - 05:54 PM

Yeah, it does. Microsoft bent over VB6 developers with that one! :D

I'll let jayman9 respond, as it sounds like he's got more current experience with it. My experience is mostly with VB6.
Was This Post Helpful? 0
  • +
  • -

#13 brottmayer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 21-January 07

Re: Opening and Reading a TXT File

Posted 12 April 2007 - 05:56 PM

View PostDilutedImage, on 12 Apr, 2007 - 05:54 PM, said:

Yeah, it does. Microsoft bent over VB6 developers with that one! :D

I'll let jayman9 respond, as it sounds like he's got more current experience with it. My experience is mostly with VB6.



OK cool thanks DilutedImage! :)
Was This Post Helpful? 0
  • +
  • -

#14 DilutedImage  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 14
  • View blog
  • Posts: 646
  • Joined: 20-November 06

Re: Opening and Reading a TXT File

Posted 12 April 2007 - 06:02 PM

I just did a quick Google search ... Check out section nine on this page:

VB .NET Programming for Beginners

It appears to explain every step of the process.

This post has been edited by DilutedImage: 12 April 2007 - 06:04 PM

Was This Post Helpful? 0
  • +
  • -

#15 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Opening and Reading a TXT File

Posted 12 April 2007 - 08:05 PM

Since you are using the .NET framework you really should be using the StreamReader and StreamWriter objects.

It looks like File operations, VB6 style, are no longer supported in the 2.0 Framework. Although at this time I cannot find anything specifically saying this in the MSDN.

The link provided by DilutedImage does a pretty good job of explaining how to use those objects to read text files.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2