writing to a text document

Talk about your favorite PC games, Steam and building awesome custom rigs!

Moderator: Moderators

ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

writing to a text document

Post by ghosstt »

im making a tictactoe game in vb.net, and im going to use a text document to store some values (for network play), but i don't know how to do it.. the only way i know how is to put stuff into a rich text document, then save from there.. is there a more official way to do it? and how do you read a specific line..
Skyone
Moderator
Posts: 6390
Joined: Tue Nov 29, 2005 8:35 pm
Location: it is a mystery
Contact:

Post by Skyone »

Use registry or ini files.
jeffslot
Portablizer Extraordinaire
Posts: 853
Joined: Wed Jun 01, 2005 6:26 am
Location: PA
Contact:

Post by jeffslot »

awesome,

give me a couple of minutes to round up some code. I do this kind of thing every day at work...


More to follow...
jeffslot
Portablizer Extraordinaire
Posts: 853
Joined: Wed Jun 01, 2005 6:26 am
Location: PA
Contact:

Post by jeffslot »

'You can replace strWorkingDirectory & "Schema.ini" with the
'name of you file to be written

Imports System.IO

Dim sw As New System.IO.StreamWriter(strWorkingDirectory & "Schema.ini")

sw.WriteLine(strSomeString)'This written string could be something
'you read in with a streamreader...

sw.Close()
sw.Dispose() 'can't really remember if you need the dispose, but give it try 1st

'Please let me know if this isn't clear or what you are looking for
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

well, im trying to use this with a form application, not the console.. so thats kinda a problem..(it doesn't work when i just copy and paste the code..)...
jeffslot
Portablizer Extraordinaire
Posts: 853
Joined: Wed Jun 01, 2005 6:26 am
Location: PA
Contact:

Post by jeffslot »

So if I get you right you're trying to write the contents of a text box or somethign, right.

can't you just put the contexts of the text box into a string
and then write that particular string instead of strSomeString.

Also you probably gonna be a bit more specific about something not working I think. Like what particular error are you getting? Thanks.

Jeff
jeffslot
Portablizer Extraordinaire
Posts: 853
Joined: Wed Jun 01, 2005 6:26 am
Location: PA
Contact:

Post by jeffslot »

Hi, I just tested this code in vb.net 2.0 successfully, let me know if it helps you.

Jeff

Code: Select all

Imports System.IO
Imports System.Text

Public Class Form1

    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sw As New StreamWriter("C:\TestOutput.txt", False)
        Dim sb As New StringBuilder

        sb.Append(TextBox1.Text)

        sw.WriteLine(sb)

        sw.Close()
        sw.Dispose()
        sb = Nothing

    End Sub
End Class
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

Sorry about that. Well, what the idea I had was, is to do this:

Game of tic-tac-toe

player 1 = 1s
player 2 = 2s (not for the visuals, just for variables sake, like when you click the box, that spot becomes a 1 or 2)

Basically, every .5 second or so, check and update the document (which is somewhere both PCs can access).

So heres the layout for the document
(the numbers refer to the line numbers)

1.spot1
2.spot2
3.spot3
4.spot4
5.spot5
6.spot6
7.spot7
8.spot8
9.spot9
10.TurnNumber
11.Player1Name
12.Player2Name
13.Player1Wins
14.Player2Wins

thats a rough sketch

now, what i want to do, is each time i say, click spot 2 as player 1 (1st row, second column) it would write a 1 in line 2. Then after about .3 of a second, both computers would read the document, and since line 2 changed, it would render the "X" or "O" on spot 2 in the form (so it checks,writes, then reads,then updates, in a continous loop every .3 seconds or so. Does that make sense?
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

here is the only error i get with that code

C:\Documents and Settings\Owner\My Documents\Visual Studio Projects\WindowsApplication1\Form1.vb(78): 'System.IO.StreamWriter.Protected Overrides Sub Dispose(disposing As Boolean)' is not accessible in this context because it is 'Protected'.
jeffslot
Portablizer Extraordinaire
Posts: 853
Joined: Wed Jun 01, 2005 6:26 am
Location: PA
Contact:

Post by jeffslot »

Do you get the same error message with the more recent code snippet?

I'm not too sure that I'm going to be able to help you. I think I understand roughly what you are up to, but as they say time is precious. What imparticular is giving you trouble about what you are trying to do?

Jeff
jeffslot
Portablizer Extraordinaire
Posts: 853
Joined: Wed Jun 01, 2005 6:26 am
Location: PA
Contact:

Post by jeffslot »

also, it should be noted that you should type something into the text box before clickin on the button.

Jeff
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

how to create a text document, write to a specific line, and read a specific line. thats all i need to know :lol: and the error is with the recent snippet
jeffslot
Portablizer Extraordinaire
Posts: 853
Joined: Wed Jun 01, 2005 6:26 am
Location: PA
Contact:

Post by jeffslot »

Hmm,

Well I might have to defer to another benheck code guru, but

You should definitely be able to

Create and write a line (writeline) to a text document using a streamwiter
Reading a document can be done using a streamreader (Readline no big surprise there)

Jeff
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

but isn't that for when using console apps?
jeffslot
Portablizer Extraordinaire
Posts: 853
Joined: Wed Jun 01, 2005 6:26 am
Location: PA
Contact:

Post by jeffslot »

I believe (and please correct me if I'm wrong someone else) you can use streamreaders & writers throughout .net.

Console
ASP
Windows

etc...
Post Reply