p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Visual Basic 2010 Programmer's Reference (http://p2p.wrox.com/forumdisplay.php?f=551)
-   -   Writing to Text File (http://p2p.wrox.com/showthread.php?t=86170)

Gary108 December 27th, 2011 06:45 PM

Writing to Text File
 
Dear Rod,

Thank you for all of the help that you have given me so far. I really appreciate it. I do have another question. I am still working on another aspect of the application that you helped me with before. The application 1) breaks down a Bitmap image into 90 lines of 64 10x10 pixel images, 2) analysis the colors to determine how many pixels are black and how many are white, 3) depending on the result, it will place either a "1" or a "0" in each of 64 text boxes which represents a series of tiny pictures that make up the first row of the original Bitmap image. I can also successfully write the contents of a set of 64 text boxes to a single line of a Text File.

My difficulty starts after I write the first line to the Text File. What I need is for each successive line of text to be written as another line of the Text File so that we would end up with 90 lines of a 64 digit number. What is actually happening is that each successive line writes over the previous line so I end up with a Text File with only one line of text representing the last line of the image. I have used "objFile.WriteLine()" in my code to add a carriage return, but it is being ignored. My code for writing to a text file is as follows:



Dim objFile As New System.IO.StreamWriter("C:\NewText.txt")
objFile.Write(strTextBox1)
objFile.Write(strTextBox2)
objFile.Write(strTextBox3)
objFile.Write(strTextBox4)
objFile.Write(strTextBox5)
objFile.Write(strTextBox6)
objFile.Write(strTextBox7)
objFile.Write(strTextBox8)
objFile.Write(strTextBox9)
objFile.Write(strTextBox10)
objFile.Write(strTextBox11)
objFile.Write(strTextBox12)
objFile.Write(strTextBox13)
objFile.Write(strTextBox14)
objFile.Write(strTextBox15)
objFile.Write(strTextBox16)
objFile.Write(strTextBox17)
objFile.Write(strTextBox18)
objFile.Write(strTextBox19)
objFile.Write(strTextBox20)
objFile.Write(strTextBox21)
objFile.Write(strTextBox22)
objFile.Write(strTextBox23)
objFile.Write(strTextBox24)
objFile.Write(strTextBox25)
objFile.Write(strTextBox26)
objFile.Write(strTextBox27)
objFile.Write(strTextBox28)
objFile.Write(strTextBox29)
objFile.Write(strTextBox30)
objFile.Write(strTextBox31)
objFile.Write(strTextBox32)
objFile.Write(strTextBox33)
objFile.Write(strTextBox34)
objFile.Write(strTextBox35)
objFile.Write(strTextBox36)
objFile.Write(strTextBox37)
objFile.Write(strTextBox38)
objFile.Write(strTextBox39)
objFile.Write(strTextBox40)
objFile.Write(strTextBox41)
objFile.Write(strTextBox42)
objFile.Write(strTextBox43)
objFile.Write(strTextBox44)
objFile.Write(strTextBox45)
objFile.Write(strTextBox46)
objFile.Write(strTextBox47)
objFile.Write(strTextBox48)
objFile.Write(strTextBox49)
objFile.Write(strTextBox50)
objFile.Write(strTextBox51)
objFile.Write(strTextBox52)
objFile.Write(strTextBox53)
objFile.Write(strTextBox54)
objFile.Write(strTextBox55)
objFile.Write(strTextBox56)
objFile.Write(strTextBox57)
objFile.Write(strTextBox58)
objFile.Write(strTextBox59)
objFile.Write(strTextBox60)
objFile.Write(strTextBox61)
objFile.Write(strTextBox62)
objFile.Write(strTextBox63)
objFile.Write(strTextBox64)
objFile.WriteLine()
objFile.Close()



In my application, the whole process is repeated 90 times using a Timer Event. My code there is:



Private Sub tmrProcesser_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrProcesser.Tick

Static CounterVariable As Integer
CounterVariable = CounterVariable + 1
txtCounterVariable.text = CounterVariable


Process()
CountPixels()
CreateTextFile()

If CounterVariable = 90 Then
tmrProcesser.Enabled = False
End If

End Sub




I've tried to make my inquiry as clear as possible without adding unnecessary code, but if it would help I can add everything. I look forward to your reply. Thanks again for all of your help.


Gary

Rod Stephens December 28th, 2011 09:09 PM

Hi Gary,

Working with files is a bit confusing in .NET because there are so many classes and so many ways you can open them. I think when you're opening the stream, it's writing a new file overwriting the old one if it exists.

I usually use CreateText, AppendText, and OpenText to create, add text at the end of, or read text files. See this example:

Open text file by using CreateText, AppendText, and OpenText in Visual Basic .NET

In your program you probably want to use AppendText so you can add the new text at the end of the existing file.

If you can, you should also try to do the writing inside a For loop instead of using a whole bunch of almost identical Write statements. That would make the code easier to work with and debug.

Gary108 December 29th, 2011 07:13 PM

Dear Rob,

Thank you for your prompt reply. I tried setting up an application in Visual Studio exactly like the example you referred me to. It is giving me two errors. One is for the Function called "AppendText". The error is: "AppendText is not declared. It may be inaccessible due to its protection level". The second error is for "txtFileName.Text". The error is the same as above.

Is the txtFileName.Text to be replaced with a specific path and file like "C:\txtFileName.Text"? If I enter a string like that I do not get an error message and it resembles the format for writing text with Streamwriter that I am familiar with.

How do I declare a function such as "AppendText" ? Thanks so much for your ongoing help. I look forward to your reply?


Regards,

Gary




Private Sub btnAppendFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAppendFile.Click

Dim stream_writer As StreamWriter
stream_writer = AppendText(txtFileName.Text)
stream_writer.WriteLine(txtText.Text)
stream_writer.Close()


End Sub

Rod Stephens December 30th, 2011 12:12 PM

Hi Gary,

Yes txtFileName is a TextBox where you can enter the name of the file for the program to manipulate.

AppendText is defined in the System.IO.File class. You can use it as in:

System.IO.File.AppendText(...)

That example includes the class in an Include statement so it can use the short version you see.

Include System.IO.File

(In general when you're looking at an example, you should download it so you can see what Includes statements it uses. (And so you can see the txtFileName TextBox and other controls.))

Gary108 December 31st, 2011 03:21 PM

Dear Rob,

Thank you for your patience with me and your ongoing help. With your last explanation I was able to successfully make my application do what it was supposed to do. There is just one thing that still confusions me. With the code below, I am able to successfully write the contents of the three text boxes below to a file named with whatever string is held in the variable, strSaveAs.

Dim stream_writer As StreamWriter
stream_writer = System.IO.File.AppendText("c:\WaterWriter\" & (strSaveAs) & ".txt")
stream_writer.Write(ImageAnalysis.TextBox1.Text)
stream_writer.Write(ImageAnalysis.TextBox2.Text)
stream_writer.Write(ImageAnalysis.TextBox3.Text)


What I do not understand is why I cannot write the text to a subdirectory of "C:\WaterWriter\" such as "C:\WaterWriter\Library\". When I try to I get an error message saying "Could not find a part of the path 'c:\WaterWriter\Library\Text.txt'. I have tried this with several different sets of directories and subdirectories and every time, if I go down to the second level directory I get that message. Can you give any insight on this matter?

Thank you again,


Gary

Rod Stephens December 31st, 2011 05:13 PM

No problem.

First a note.

Code:

stream_writer = System.IO.File.AppendText("c:\WaterWriter\" & (strSaveAs) & ".txt")
Here the parentheses around strSaveAs aren't necessary and confuse things a little bit. What parentheses mean to VB in this situation is, "Evaluate the enclosed thing and use the result." So here it's actually creating a new temporary variable with the same value as strSaveAs and using that. It doesn't make any real difference in this case but there are situations where that can be confusing so I try to avoid unnecessary parentheses.

But to the main question. My suspicion is that C:\WaterWriter\Library\ doesn't already exist. The AppendText method will create the file if it doesn't already exist but I don't think it will create directories if they're not already there.

One way to create the directory is to do something like this:

Code:

My.Computer.FileSystem.CreateDirectory("C:\WaterWriter\Library\")
If the directory already exists, it should quietly return without throwing an error.

I think it can even create intermediary directories. For example, if C:\DirA exists, then it can create C:\DirA\DirB\DirC\DirD all at once.


All times are GMT -4. The time now is 01:46 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.