From: Keith (Southend)G on 29 Dec 2009 11:31 I'm trying to get the resulting text to open up in RichTextBox2, the first bit works fine but the second bit 'I.O.StreamWriter' I can't seem to find the correct code. I can get the "Text Written to File" window pop up, but not with the code in the second set below. I have manually created at "sorted.txt" file which I would expect to appear in RichTextBox2, but it doesn't. Any help would be great. Thanks Keith (Southend) 1st set Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim FILE_NAME As String = "C:\ogimet.txt" If System.IO.File.Exists(FILE_NAME) = True Then Dim objReader As New System.IO.StreamReader(FILE_NAME) RichTextBox1.Text = objReader.ReadToEnd objReader.Close() Else MsgBox("File Does Not Exist") End If End Sub 2nd Set Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim FILE_NAME As String = "C:\sorted.txt" If System.IO.File.Exists(FILE_NAME) = True Then Dim objWriter As New System.IO.StreamWriter(FILE_NAME) RichTextBox2.Text = objWriter.Write objWriter.Close() MsgBox("Text written to file") Else MsgBox("File Does Not Exist") End If End Sub
From: Captain Jack on 29 Dec 2009 11:53 "Keith (Southend)G" <keith_harris9(a)hotmail.com> wrote in message news:72d7c610-79ad-47af-83e1-474035633c5b(a)s31g2000yqs.googlegroups.com... > Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles Button2.Click > Dim FILE_NAME As String = "C:\sorted.txt" > > If System.IO.File.Exists(FILE_NAME) = True Then > Dim objWriter As New System.IO.StreamWriter(FILE_NAME) > > RichTextBox2.Text = objWriter.Write > > objWriter.Close() > MsgBox("Text written to file") > Else > MsgBox("File Does Not Exist") > End If > End Sub The Write method of the StreamWriter class doesn't return a value; in VB terms, it's implemented as a Sub. There wouldn't be any data for it to assign to the Text property of the RichTextBox control. Also, I can't find an overload of the method that accepts no arguments, so I'm not sure how your objWriter.Write call isn't generating a compile error. Normally, what I'd do would be to build a string of data that was going to the file, write it through the StreamWriter instance, and separately copy it to the display control (the RichTextBox, in this case). -- Jack
From: Keith (Southend)G on 30 Dec 2009 12:53 On Dec 29, 4:53 pm, "Captain Jack" <CaptainJack1...(a)comcast.net> wrote: > > The Write method of the StreamWriter class doesn't return a value; in VB > terms, it's implemented as a Sub. There wouldn't be any data for it to > assign to the Text property of the RichTextBox control. Also, I can't find > an overload of the method that accepts no arguments, so I'm not sure how > your objWriter.Write call isn't generating a compile error. > > Normally, what I'd do would be to build a string of data that was going to > the file, write it through the StreamWriter instance, and separately copy it > to the display control (the RichTextBox, in this case). > > -- > Jack Thanks Jack. I'm probably not approaching this this best way, but hoping to reach a position where I get the file(s) read and written to where I want them and then I can start 'playing with the parsing bit, which is the bit that does all the tricks. I've moved on a little and only get one error message, although pressing F5 it runs. Mike previously gave me a start with some of that basic code, but I suspect the 'observation' bit I need to rename or rename something else... Warning 1 Variable 'observation' is used before it has been assigned a value. A null reference exception could result at runtime. C: \Documents and Settings\Keith\My Documents\Visual Studio 2008\Projects \Open Text\Open Text\Form1.vb 46 39 Open Text The code now looks like this: It is probably obvious once you know, but what happens is I click on button 1 "Get Text" and in the RichTextBox1 opens up my 'ogimet.txt' file with all the numbers in it. Now when I click on "Sort Text" no errors come up but in RixhTextBox2 there is nothing, which is probably because my parsing as it stands leaves nothing, but when I look at my original "ogimet.txt" file it is now empty 0 KB. What's causing this? I know you mentioned 'overload' with 'objWriter.Write', not sure what this is. An example of what 'ogimet.txt' looks like can be found here.... http://www.southendweather.net/ogimet.txt (This file I update every 12 hours or so and the creation of this will be all part of this project, however, one thing at a time). Many thanks. Keith <snip> Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim FILE_NAME As String = "C:\ogimet.txt" If System.IO.File.Exists(FILE_NAME) = True Then Dim objReader As New System.IO.StreamReader(FILE_NAME) RichTextBox1.Text = objReader.ReadToEnd objReader.Close() Else MsgBox("File Does Not Exist") End If End Sub and... Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim FILE_NAME As String = "C:\ogimet.txt" Dim observation As String If System.IO.File.Exists(FILE_NAME) = True Then Dim objWriter As New System.IO.StreamWriter(FILE_NAME) Using fs As New System.IO.FileStream _ ("C:\sorted.txt", System.IO.FileMode.Open) Dim sr As New System.IO.StreamReader(fs) Dim input As String = sr.ReadLine() While (input <> Nothing) If (Not input.StartsWith("#")) Then observation = observation & " " & input End If If (observation.EndsWith("=")) Then ' parse the line here... ' set the current observation to blank to start a new run observation = "" End If input = sr.ReadLine() End While End Using objWriter.Close() MsgBox("Text written to file") Else MsgBox("File Does Not Exist") End If End Sub End Class <snip>
From: Captain Jack on 30 Dec 2009 16:06 "Keith (Southend)G" <keith_harris9(a)hotmail.com> wrote in message news:63d8981a-7380-4ff2-b751-ebfa88c39d2c(a)r24g2000yqd.googlegroups.com... > Thanks Jack. > > I'm probably not approaching this this best way, but hoping to reach a > position where I get the file(s) read and written to where I want them > and then I can start 'playing with the parsing bit, which is the bit > that does all the tricks. I've moved on a little and only get one > error message, although pressing F5 it runs. Mike previously gave me a > start with some of that basic code, but I suspect the 'observation' > bit I need to rename or rename something else... > > Warning 1 Variable 'observation' is used before it has been assigned a > value. A null reference exception could result at runtime. C: > \Documents and Settings\Keith\My Documents\Visual Studio 2008\Projects > \Open Text\Open Text\Form1.vb 46 39 Open Text > > The code now looks like this: > It is probably obvious once you know, but what happens is I click on > button 1 "Get Text" and in the RichTextBox1 opens up my 'ogimet.txt' > file with all the numbers in it. Now when I click on "Sort Text" no > errors come up but in RixhTextBox2 there is nothing, which is probably > because my parsing as it stands leaves nothing, but when I look at my > original "ogimet.txt" file it is now empty 0 KB. What's causing this? > I know you mentioned 'overload' with 'objWriter.Write', not sure what > this is. An example of what 'ogimet.txt' looks like can be found > here.... > http://www.southendweather.net/ogimet.txt > (This file I update every 12 hours or so and the creation of this will > be all part of this project, however, one thing at a time). > > Many thanks. Keith > > <snip> > Public Class Form1 > > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e > As System.EventArgs) Handles Button1.Click > Dim FILE_NAME As String = "C:\ogimet.txt" > If System.IO.File.Exists(FILE_NAME) = True Then > Dim objReader As New System.IO.StreamReader(FILE_NAME) > > RichTextBox1.Text = objReader.ReadToEnd > > objReader.Close() > Else > MsgBox("File Does Not Exist") > End If > End Sub > > and... > > Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles Button2.Click > Dim FILE_NAME As String = "C:\ogimet.txt" > Dim observation As String > If System.IO.File.Exists(FILE_NAME) = True Then > Dim objWriter As New System.IO.StreamWriter(FILE_NAME) > > > Using fs As New System.IO.FileStream _ > ("C:\sorted.txt", System.IO.FileMode.Open) > > > Dim sr As New System.IO.StreamReader(fs) > Dim input As String = sr.ReadLine() > While (input <> Nothing) > > > If (Not input.StartsWith("#")) Then > observation = observation & " " & input > End If > > > If (observation.EndsWith("=")) Then > ' parse the line here... > > > ' set the current observation to blank to > start a new run > observation = "" > End If > > > input = sr.ReadLine() > End While > End Using > objWriter.Close() > MsgBox("Text written to file") > Else > MsgBox("File Does Not Exist") > End If > End Sub > End Class > <snip> Cool... Item the first: In your code, you do have the potential for an error, but the concatenation operator ("&" symbol) is saving you. When you execute ==> observation = observation & " " & input <== the first time, the variable "observation" has no value (that is, it is Nothing) but the operation as written is treating it like an empty string. A safer way to avoid errors and to get rid of the warning would be to change your declaration to this: Dim observation As String = "" Which initializes the variable to a value at the same time as it's declared. Second: Your original file is being erased because you're opening a StreamWriter on it in the Button2 event handler, here: Dim objWriter As New System.IO.StreamWriter(FILE_NAME) Because FILE_NAME was set to the the same file name that you were originally reading a few lines above that. The StreamWriter is emptying out the file in preparation for sending data to it. Since there's no code (in this sample, anyway) that's sending any data down the StreamWriter, the file ends up empty. Third: A function or sub is said to be "overloaded" when there's more than one way to pass arguments to it. For example, imagine a Sub defined like this: Public Sub Drink(Item As BeverageType) End Sub That is also defined this way: Public Sub Drink(Item As BeverageType, OnlyIfThirsty As Boolean) End Sub These variations of the same function let you simplify the code, control how the function is called, and control what the function does when it's called in different ways. In your previous example, you were calling the Write method of a class and assigning the result to the Text property of RichTextBox2. In your example, the Write method didn't have any arguments in the call, and I couldn't find any definition (overload) of the Write method that didn't take arguments, so I wasn't sure how you weren't getting an error. Okay, all that out of the way... I'm not completely sure what you're trying to accomplish overall here. It kinda looks like you want to read some data from a text file, find lines that match a pattern, parse some data out of those lines, and write the data back. Am I on track here? If you describe the steps of the problem you're trying to solve (without regard to how it would be done in VB) I'd be happy to try to suggest some VB code for doing that. -- Jack
From: Keith (Southend)G on 30 Dec 2009 17:04
On Dec 30, 9:06 pm, "Captain Jack" <CaptainJack1...(a)comcast.net> wrote: > "Keith (Southend)G" <keith_harr...(a)hotmail.com> wrote in message > Okay, all that out of the way... I'm not completely sure what you're trying > to accomplish overall here. It kinda looks like you want to read some data > from a text file, find lines that match a pattern, parse some data out of > those lines, and write the data back. Am I on track here? If you describe > the steps of the problem you're trying to solve (without regard to how it > would be done in VB) I'd be happy to try to suggest some VB code for doing > that. > > -- > Jack Yes, your description just about sums up what I'm trying to do. Ok Jack, I'll try to explain what I'm trying to achieve, but I imagine it will be more involved, I maybe asking to much of you. Even so, as there is so much that I'm sure I could do I am keen to pick VB up and develop things further, just need to get a head start, see some examples etc. Basically, the idea is to save a lot of time I spend doing things manually. It's a hobby I've had since I was 17, I'm now knocking on 50 :-) There are thousands of locations that provide synop code (weather), but for this example I will use 2 locations, the first is the nearest to where I live and the other is just randomly chosen from Spain. What I want to do is have a separate file for each time. 1. The date/time '200912301350' etc can be ignored. 2. The date/time of importance is the AAXX 30144 etc. (AAXX means land station, 30 is date (30th) 14 is time (14:00), the last '4' can be ignored, this can sometimes be a 0, 1, 3 or 4, it tells you whether windspeed is reported in mph, mps or knots. So AAXX 30094 is 30th 09:00 hours etc. 3. The numbers 03693 or 08360 is the wmo (World Meteorological Organisations) station number, this number I would want at the start of each line. 4. The '=' signifies the end of each line of code. 5. The resulting file(s) / file names I want to then look like, see bottom of this post... ################################################################################ # SYNOPS from 03693, Shoeburyness (United Kingdom) | 51-33N | 000-50E | 2 m ################################################################################ 200912301350 AAXX 30144 03693 46244 /0412 10046 20046 39920 49922 53001 91350 333 88/04= 200912301250 AAXX 30134 03693 46250 /0512 10048 20048 39914 49917 50001 91250 333 88/04= 200912301150 AAXX 30124 03693 16250 /0411 10049 20048 39916 49919 50007 69921 91150 333 88/05= 200912301050 AAXX 30114 03693 46259 /0612 10051 20049 39918 49920 51016 91050 333 88/05= 200912300950 AAXX 30104 03693 46259 /0512 10050 20049 39914 49916 53014 90950 333 88/05= 200912300850 AAXX 30094 03693 46262 /0511 10051 20051 39910 49912 53013 90850 333 88/05= ################################################################################ # SYNOPS from 08360, Alicante / El Altet (Spain) | 38-17N | 000-33W | 43 m ################################################################################ 200912301400 AAXX 30144 08360 46/// /1704 10230 20138 30016 40051 56029 555 60005= 200912301300 AAXX 30134 08360 46/// /1907 10213 20131 30026 40062 58021 555 60005= 200912301200 AAXX 30124 08360 02681 32305 10222 20110 30032 40068 58014 60001 82202 333 60007= 200912301100 AAXX 30114 08360 46/// /2206 10207 20122 30045 40081 50005 555 60005= 200912301000 AAXX 30104 08360 46/// /2003 10191 20121 30047 40083 51010 555 60005= 200912300900 AAXX 30094 08360 22681 30000 10150 20116 30046 40083 53014 82801 333 60007= 5. continued... Resulting files to look like this..... and saved into a specified directory, can be C:/**********. The 2009 (year) and 12 (month) could be taken from the original text or from the computers clock. File name say: 2009123009.txt 03693 46262 /0511 10051 20051 39910 49912 53013 90850 333 88/05= 08360 22681 30000 10150 20116 30046 40083 53014 82801 333 60007= 2009123010.txt 03693 46259 /0512 10050 20049 39914 49916 53014 90950 333 88/05= 08360 46/// /2003 10191 20121 30047 40083 51010 555 60005= 2009123011.txt 03693 46259 /0612 10051 20049 39918 49920 51016 91050 333 88/05= 08360 46/// /2206 10207 20122 30045 40081 50005 555 60005= 2009123012.txt 03693 16250 /0411 10049 20048 39916 49919 50007 69921 91150 333 88/05= 08360 02681 32305 10222 20110 30032 40068 58014 60001 82202 333 60007= 2009123013.txt 03693 46250 /0512 10048 20048 39914 49917 50001 91250 333 88/04= 08360 46/// /1907 10213 20131 30026 40062 58021 555 60005= 2009123014.txt 03693 46244 /0412 10046 20046 39920 49922 53001 91350 333 88/04= 08360 46/// /1704 10230 20138 30016 40051 56029 555 60005= To give you an idea of how many locations there are look here: http://www.southendweather.net/2009123018.txt This is a file from 18:00z this evening. Many thanks Keith (Southend) email: kreh(a)southendweather.net |