Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Visual Basic 2010 Programmer's Reference
This is the forum to discuss the Wrox book Visual Basic 2010 Programmer's Reference by Rod Stephens; ISBN: 9780470499832
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Visual Basic 2010 Programmer's Reference section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 27th, 2011, 06:45 PM
Authorized User
Points: 193, Level: 3
Points: 193, Level: 3 Points: 193, Level: 3 Points: 193, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #2 (permalink)  
Old December 28th, 2011, 09:09 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,187, Level: 23
Points: 3,187, Level: 23 Points: 3,187, Level: 23 Points: 3,187, Level: 23
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jan 2006
Location: , , .
Posts: 646
Thanks: 2
Thanked 96 Times in 95 Posts
Default

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.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
The Following User Says Thank You to Rod Stephens For This Useful Post:
Gary108 (December 31st, 2011)
  #3 (permalink)  
Old December 29th, 2011, 07:13 PM
Authorized User
Points: 193, Level: 3
Points: 193, Level: 3 Points: 193, Level: 3 Points: 193, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Question

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
Reply With Quote
  #4 (permalink)  
Old December 30th, 2011, 12:12 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,187, Level: 23
Points: 3,187, Level: 23 Points: 3,187, Level: 23 Points: 3,187, Level: 23
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jan 2006
Location: , , .
Posts: 646
Thanks: 2
Thanked 96 Times in 95 Posts
Default

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.))
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
The Following User Says Thank You to Rod Stephens For This Useful Post:
Gary108 (December 31st, 2011)
  #5 (permalink)  
Old December 31st, 2011, 03:21 PM
Authorized User
Points: 193, Level: 3
Points: 193, Level: 3 Points: 193, Level: 3 Points: 193, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Question

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
Reply With Quote
  #6 (permalink)  
Old December 31st, 2011, 05:13 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,187, Level: 23
Points: 3,187, Level: 23 Points: 3,187, Level: 23 Points: 3,187, Level: 23
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jan 2006
Location: , , .
Posts: 646
Thanks: 2
Thanked 96 Times in 95 Posts
Default

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.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing to Text File striker9 ASP.NET 2.0 Basics 1 May 12th, 2006 06:17 AM
problem writing dynamic value into text file in ph method PHP How-To 0 February 6th, 2006 02:05 PM
writing text file but in an order specifed... stone XSLT 2 October 27th, 2005 06:17 AM
Reading from a text file and writing to a Binary 1 scott01 C++ Programming 0 July 28th, 2003 04:59 AM



All times are GMT -4. The time now is 02:28 AM.


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