Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

re.using text files in vb.net

Status
Not open for further replies.

DGEN

Programmer
Oct 29, 2004
17
GB
Hi All

what would be the VB.net equivelent of this code from

sams teach yourself vb6 in 21 days

1: Private Sub cmdFile_Click()
2: Dim intCtr As Integer ‘ Loop counter
3: Dim intFNum As Integer ‘ File number
4: Dim intMsg As Integer ‘ MsgBox() return
5: intFNum = FreeFile
6: ‘ Change the path if you want
7: Open “C:\Print.txt” For Output As #intFNum
8:
9: ‘ Describe this proc
10: intMsg = MsgBox(“File Print.txt opened”)
11:
12: For intCtr = 1 To 6
13: Print # intFNum, intCtr; ‘ Notice semicolon!!
14: intMsg = MsgBox(“Writing a ” & intCtr & “ to Print.txt”)
15: Next intCtr
16:
17: Close # intFNum
18:
19: intMsg = MsgBox(“File Print.txt closed”)
20: End Sub

as i liked using this code as a baseline in my vb6 project
sny advice would be greatful
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim sw As New System.IO.StreamWriter(“C:\Print.txt”)

MsgBox(“File Print.txt opened”)

For intCtr As Integer = 1 To 6
sw.WriteLine(intCtr)
MsgBox(“Writing a ” & intCtr & “ to Print.txt”)
Next

sw.Close()

MsgBox(“File Print.txt closed”)
End Sub

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Hi Thank you for your quick reply here is another
piece of code that i used in a previous program this code loads a file for reading and also i found it useful for my card game - why did they have to change vb so much


16: Private Sub cmdFileIn_Click ()
17: ‘ Read the sequential file
18: Dim intCtr As Integer ‘ Loop counter
19: Dim intVal As Integer ‘ Read value
20: Dim intFNum As Integer ‘ File number
21: Dim intMsg As Integer ‘ MsgBox()
22: intFNum = FreeFile
23: Open “Print.txt” For Input As #intFNum
24:
25: For intCtr = 1 To 6
26: Input # intFNum, intVal
27: ‘ Display the results in the Immediate window
28: intMsg = MsgBox(“Retrieved a ” & intVal & “ from Print.txt”)
29: Next intCtr
30:
31: Close # intFNum
32: intMsg = MsgBox(“The Print.txt file is now closed.”)
 
It's basically the same, just use a StreamReader instead of a StreamWriter:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim intVal As Integer
Dim [red]sr[/red] As New System.IO.[red]StreamReader[/red](“C:\Print.txt”)

MsgBox(“File Print.txt opened”)

For intCtr As Integer = 1 To 6
intVal = sr.ReadLine
MsgBox("Retrieved a ” & intCtr & “ from Print.txt”)
Next

sr.Close()

MsgBox(“File Print.txt closed”)
End Sub

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Hi Thank you for that

Finaly i would like to know what the VB.net equivelent to the code i placed on my last post was but as a sequential file as in "1,2,3,4,5,6" instead of a new line for each

what i mean is that i would like to know how i would go about reading the same file as previous but saying that the newline is a comma and not the end of the line

1, 2, 3, 4, 5, 6, 7

as opposed to
1
2
3
4
5
6
7
 
To get a string array
Code:
dim s as string()="1,2,3,4,5,6,7".split(","c)


Sweep
...if it works, you know the rest..
Always remember that Google is your friend

curse.gif
 
HI
That was not the method i was looking for but it useful nonetheless

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim intVal As Integer
Dim sr As New System.IO.StreamReader(“C:\Print.txt”)

MsgBox(“File Print.txt opened”)

For intCtr As Integer = 1 To 6
intVal = sr.ReadLine
MsgBox("Retrieved a ” & intCtr & “ from Print.txt”)
Next

sr.Close()

MsgBox(“File Print.txt closed”)
End Sub

Where we have intVal = sr.Readline

I want it to fine a comma in the file thus meaning it is a new line.

thank you anyways
 
Sorry that last post made no sense

ok i have files that contain lines of stats for each of the cards that is in that deck of cards

eg

Card Name.txt | Stats.txt
---------------------------------
Dark Warior | 03,03,04,02,33,01
Necromancer | 01,01,03,01,05,00
Dark Dragon | 11,11,11,09,99,09
Toramis | 10,09,09,10,70,08

That is an example of the link between cards, i would like to know how i would be able to program the software to read each of those variables in stats.txt and use the seperator to set arrays that have been put into VB.net

eg. readline - seperator = "," - A=A+1 - if A=1 then
Armour(intVal) = 03 - Continue till next "comma" - if A =2 then Strength(intVal) = 3 - Continue till next comma

I know this seems quite technical but it is the best way i can describe the desired results that i am looking for

When it then treaches the end of the line A gets reset and intVal goes onto the next number in its for step.

all i need to know is how to distinguish this comma as a seperator i can then do the rest so i am not looking for a complete piece of code here just the result

If this helps or not then i will be greatful for a reply
cheers

Ps Sorry for the lengthy post

 
From the sounds of it, you're in the process of learning, so it might help to see multiple ways to do the same thing.

One way to do it is to use StreamReader's ReadLine method to put the full line into a string. Then you could go character by character through the string using the string .char(i) method. This is only one way to skin this cat :p (EVERY pun intended)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top