2 Replies - 1709 Views - Last Post: 26 August 2008 - 10:46 PM Rate Topic: -----

#1 fenrisulfr  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 16-August 08

Reading and writing text files...

Posted 26 August 2008 - 09:12 PM

So i read this tutorial:
http://www.dreaminco...wtopic40107.htm
and this one :
http://www.builderau...ndow...20267367,00.htm

And now im hopelessly confused....
I got the program to create the text file but i cant get it to add the text file name to a master list...
	If File.Exists("C:\Tempest Temp Files\" & tbxNationName.Text & ".txt") = False Then
			IO.File.CreateText("C:\Tempest Temp Files\" & tbxNationName.Text & ".txt")

			Dim writer As New System.IO.StreamWriter("C:\Tempest Temp Files\nationmasterlist.txt")
			'write the file with the new line
			writer.WriteLine(tbxNationName.Text)
			writer.Close()
		Else
			MessageBox.Show("File Already Exists")
		End If



i get the file created but no text appears in the master list

Any advice?

This post has been edited by fenrisulfr: 26 August 2008 - 09:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Reading and writing text files...

#2 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: Reading and writing text files...

Posted 26 August 2008 - 10:33 PM

Try this
	If File.Exists("C:\Tempest Temp Files\" & tbxNationName.Text & ".txt") = False Then
			IO.File.CreateText("C:\Tempest Temp Files\" & tbxNationName.Text & ".txt")

			Dim writer As New System.IO.StreamWriter("C:\Tempest Temp Files\nationmasterlist.txt")
			'write the file with the new line
			writer.WriteLine(writer.ReadToEnd() & tbxNationName.Text)
			writer.Close()
		Else
			MessageBox.Show("File Already Exists")
		End If



Here is the line I changed
From: writer.WriteLine(tbxNationName.Text)
To: writer.WriteLine(writer.ReadToEnd() & tbxNationName.Text)

Hope this helps
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Reading and writing text files...

Posted 26 August 2008 - 10:46 PM

WriteLine will append to the end of a file normally, but in this situation you have to keep in mind that opening the file with StreamWriter using a string path to a file that exists is going to erase the file before writing to it. So if there is data in it, that will be erased, but it should still write whatever the content in tbxNationName.Text so make sure there is content there.

I also notice you create two different files here. The first one is to a file named whatever you typed in tbxNationName which you then don't write to and then another where you create a file called nationmasterlist.txt which you are then writing to. So make sure that is what you want to do. (Just in case you typed in something like "example" into tbxNationName and expecting to see output in example.txt because there won't be. You never wrote to it.

So hopefully that helps you out. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1