Struggling
Struggling
Struggling
Struggling
Struggling
Struggling Struggling Struggling Struggling Struggling Struggling Struggling Struggling
Struggling Struggling
Struggling
Go Back  Xtreme Visual Basic Talk > > > Struggling


Reply
 
Thread Tools Display Modes
  #1  
Old 05-09-2011, 09:58 AM
henry0811 henry0811 is offline
Newcomer
 
Join Date: May 2011
Posts: 15
Default Struggling


Hi guys, I really hope someone can help me with this one as I'm really stuck. I've been trying to work this out for the last 3 days and I'm getting no where! I've done some searching but I can't find any code that works for me So..

I have a text file C:\test.txt

Inside this text file I have these lines..

hello
world
no
how
are
you

Now, on my form I have a combobox1 with Yes and No options which the user can select.

I want to be able to click my apply button in my app and change the "no" to a "yes" in test.txt if "Yes" is selected in the combobox and visa versa, if "No" is in the comobox, I'd want yes to overwrite no in test.txt.

My application is almost complete, this is all I'm stuck with. Any help would be greatly appreciated.

I'm using VB2008.

Thanks.
Reply With Quote
  #2  
Old 05-09-2011, 02:12 PM
Gruff's Avatar
GruffStruggling Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

If you couldn't find it you must not have been looking that hard.

The tools to read and write to files in vb.net are located under the namespace:

System.IO.File

Start there and do some reading about the methods and properties available to you.

Basically you will be reading in the file contents, making your change, then overwriting the entire file.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #3  
Old 05-09-2011, 02:51 PM
henry0811 henry0811 is offline
Newcomer
 
Join Date: May 2011
Posts: 15
Default

Hi gruff and thanks for the reply! I can read in the file contents and store them in a variable but I'm having a hard time writing the change back to the file correctly.

I can successfully overwrite "no" with a "yes" in the text file, however, if I choose No in the combobox, it will write no to the textfile but keep the yes and visa versa. I must be missing something really simple here. This is what I have at the moment:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim Reader As New IO.StreamReader("C:\Users\Gaming\Desktop\t.txt")
Dim DataLines As New System.Collections.Specialized.StringCollection
Dim CurrentLine As String

If ComboBox9.Text.Contains("Yes") Then
Do While Reader.EndOfStream = False
CurrentLine = Reader.ReadToEnd
If CurrentLine.Contains("yes") = False Then
DataLines.Add(CurrentLine)
End If
Loop
Reader.Close()
Reader = Nothing

Dim Writer As New IO.StreamWriter("C:\Users\Gaming\Desktop\t.txt")
For Each CurrentLine In DataLines
Writer.WriteLine(CurrentLine)
Next
Writer.WriteLine("yes")
Writer.Close()

ElseIf ComboBox9.Text.Contains("No") Then
Do While Reader.EndOfStream = False
CurrentLine = Reader.ReadToEnd
If CurrentLine.Contains("no") = False Then
DataLines.Add(CurrentLine)
End If
Loop
Reader.Close()
Reader = Nothing

Dim Writer As New IO.StreamWriter("C:\Users\Gaming\Desktop\t.txt")
For Each CurrentLine In DataLines
Writer.WriteLine(CurrentLine)
Next
Writer.WriteLine("no")
Writer.Close()
End If
End Sub

Regards.
Reply With Quote
  #4  
Old 05-09-2011, 03:03 PM
henry0811 henry0811 is offline
Newcomer
 
Join Date: May 2011
Posts: 15
Default

Ignore te above code, it's complete and utter fail lol.
Reply With Quote
  #5  
Old 05-09-2011, 04:05 PM
henry0811 henry0811 is offline
Newcomer
 
Join Date: May 2011
Posts: 15
Default

Ok, I've read the textfile into a variable...

Dim ioFile As New StreamReader("C:\Users\Gaming\Desktop\t.txt")

Dim ioLine As String ' Going to hold one line at a time
Dim ioLines As String ' Going to hold whole file

ioLine = ioFile.ReadToEnd
ioLines = ioLine
While Not ioLine = ""
ioLine = ioFile.ReadToEnd
ioLines = ioLines & vbCrLf & ioLine
End while

I'm just having trouble changing no to a yes in the textfile, and visa versa.
Reply With Quote
  #6  
Old 05-09-2011, 04:36 PM
PlausiblyDamp's Avatar
PlausiblyDampStruggling PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

I am really struggling to understand what the code you posted is trying to do....
You are reading the entire file into a string variable, then making another variable equal to this one, then in a loop you are constantly reading the file and repeatedly appending the contents of the file to the ioLines variable....

You could read the entire file into a single string by using the .ReadToEnd method and then simply use the string's .Split method to split the string into an array of strings using the appropriate line terminator (probably a CR + LF).

Once the strings are in the array it should be easy enough to loop through the array looking for the value you are after.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #7  
Old 05-09-2011, 04:48 PM
henry0811 henry0811 is offline
Newcomer
 
Join Date: May 2011
Posts: 15
Default

I know PlausiblyDamp, I'm trying to understand it too :P I'm really new to vb.

All I want to do is replace "no" with a "yes" in a text file using a combobox and a button. I've been reading for hours now but I just can't get it right

EDIT: And all the code examples I find for this are about 10 years old.
Reply With Quote
  #8  
Old 05-09-2011, 05:02 PM
PlausiblyDamp's Avatar
PlausiblyDampStruggling PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

So did you follow either of the links I posted?

You already know how to read the file into a string, the .Split method is pretty straightforward as well - did you manage to get those bits working?

If you can get the file contents into a string array then looping over the array is nothing more than a For Each Loop and checking each entry in turn - once you find the entry you need to change simply assign it a new value.

In fact if the term you are trying to replace only exists once in the file you could just load it in with the .ReadToEnd, then use the string's .Replace method to change the value and then write the entire string back out with a StreamWriter.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #9  
Old 05-09-2011, 05:09 PM
henry0811 henry0811 is offline
Newcomer
 
Join Date: May 2011
Posts: 15
Default

Sorry, I didn't notice they were links mate. My head is banging at the moment lol.

I've spent ages designing my form, I have over 20 comboboxes that are also connected to a 'load' button, when clicked it successfully reads through my textfile searching for specific strings and placing them into the comboboxes, however, I don't understand the above mate
Reply With Quote
  #10  
Old 05-09-2011, 05:22 PM
PlausiblyDamp's Avatar
PlausiblyDampStruggling PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Which bits don't you understand? Have you read the links and tried to apply them in your code yet?
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #11  
Old 05-09-2011, 05:36 PM
henry0811 henry0811 is offline
Newcomer
 
Join Date: May 2011
Posts: 15
Default

I'm just unable to figure this part out mate, I know you're trying to help guide me in the right direction and I appreciate it I really do mate but I've been trying to figure it out on my own since lastweek and now I'm totally confused, more so at the fact that this is probably so easy to do and I'm completely missing it.
Reply With Quote
  #12  
Old 05-09-2011, 06:01 PM
PlausiblyDamp's Avatar
PlausiblyDampStruggling PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Is there a particular part that isn't working?

When solving a problem you need to break it down into simpler, easier to solve steps - this is pretty much the basic idea of actually writing code.

If you look at your problem it can be broken down into a series of simpler steps.
  • Firstly you need to read the contents of the file into a variable - you already know how to do this bit as in the code above you are doing
    Code:
    ioLine = ioFile.ReadToEnd
    which does exactly what you need.
  • The next part involves turning this simple string variable into a series of individual strings - one for each line in the file; this can be done by the .Split method of the string variable (link is posted above). Simply call .Split on the ioFile variable passing in the correct line terminator (probably a CrLf pair) and assign it's result to a variable declared as an array of strings.
  • By now you should have an array of strings, one for each line in the original file, if you are going to write out a file you would need to use a StreamWriter.
  • You can now simply loop over the array using a For Each loop (again link above) and for each item in the array you can check if it is the value you are interested in - if not just write it to the output file, if it is the value you are interested in then write out the new value instead.
  • Once the loop has exited simply close both the StreamReader and StreamWriter and everything should be done.
If you try implementing the above steps and there are problems then feel free to ask again, it would really help people to understand the problem if you can post the code you have so far and specifics of the problems you are having.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #13  
Old 05-09-2011, 06:10 PM
Gruff's Avatar
GruffStruggling Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Are you aware that a text file has characters you cannot see that define the end of a line of text and the start of a new line?
These are known as the Carriage-Return (CR) and the Line-Feed (LF) characters. Their ASCII values are 13 and 10.
If you use the Reader.ReadToEnd method you are reading the entire file including those characters into your string variable.

What PD is suggesting is that you read the entire file into one string variable. then use the String.Split() command to break the string up into an array of strings that structurally looks like the file. You do this by using the CRLF characters as the delimiters in the Split command.

Then use a "For .. Next" loop to iterate through the string array looking for either "yes" or "no"
Change just that one array element (Line of text) and write the whole thing back to the file.

In pseudo-code:

Code:
Open File
    Read Entire File into SingleString
Close File

StringArray() = SingleString.Split( CR, LF)

For Each Line in StringArray()
   If Combobox = "yes" then
       if Line = "no" then Line = "yes" 
   Elseif Combobox = "no" then
       if Line = "yes" then Line = "no" 
   End if 
Next Line

Combine StringArray back into a SingleString

Open File
   Write SingleString to File replacing original
Close File
Whoops!! Sorry PD. Didn't know your conversation was still going. I'll back out.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #14  
Old 05-09-2011, 06:19 PM
henry0811 henry0811 is offline
Newcomer
 
Join Date: May 2011
Posts: 15
Default

Thanks for the replies mate.

Here is exactly what I have...

I have a button on my form that reads my textfile and places several seperate strings I want into different comboboxes...

Dim ioFile As New StreamReader("C:\Users\Gaming\Desktop\t.txt")

Dim ioLine As String ' Going to hold one line at a time
Dim ioLines As String ' Going to hold whole file
Dim arytext() As String

ioLine = ioFile.ReadToEnd
ioLines = ioLine
While Not ioLine = ""
ioLine = ioFile.ReadToEnd
ioLines = ioLines & vbCrLf & ioLine

If ioLines.Contains("yes") Then

ComboBox9.Text = "Yes"

ElseIf ioLines.Contains("no") Then

ComboBox9.Text = "No"

End If

ioFile.Close()
End While

TextBox2.Text = ("loaded!")


This is working great. However, I've read, read, read and read alot but I can't get the crucial (and final) part of my first app working... The apply button. If my textfile contains "no" and the user chooses "yes" in the combobox, I want it to overwrite "no" with "yes" and visa versa. I was trying the code above (first code post) but was getting no where so I deleted it.
Reply With Quote
  #15  
Old 05-09-2011, 06:24 PM
PlausiblyDamp's Avatar
PlausiblyDampStruggling PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

That code looks remarkably like the code you originally posted and doesn't seem to have any of the suggestions from myself and Gruff in there though.

Did you attempt to use .Split anywhere? Did you try to loop over the resulting array?
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #16  
Old 05-09-2011, 06:31 PM
henry0811 henry0811 is offline
Newcomer
 
Join Date: May 2011
Posts: 15
Default

That code is from my load button, it won't be changing anything, it just searches for specific strings and places them in comboboxes so the user can see what they have in the textfile, yes or no.

I tried to use the same code to read the file again for my apply new settings button...

Dim ioFile As New StreamReader("C:\Users\Gaming\Desktop\t.txt")

Dim ioLine As String ' Going to hold one line at a time
Dim ioLines As String ' Going to hold whole file
Dim arytext() As String

ioLine = ioFile.ReadToEnd
ioLines = ioLine
While Not ioLine = ""
ioLine = ioFile.ReadToEnd
ioLines = ioLines & vbCrLf & ioLine

This is reading the file again isn't it? Getting it ready to change "yes" to "no" and visa versa?

I just know know what to put after it.
Reply With Quote
  #17  
Old 05-09-2011, 06:47 PM
PlausiblyDamp's Avatar
PlausiblyDampStruggling PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

You don't need to read the file twice though, neither is there any point in reading it into one variable and then just copying it to another.

All the steps you need are given above, in the correct order and with links to the documentation for the actual methods.

None of the code you just posted seems to make any attempt to split the string, loop over the string or write out the modified file. Have you attempted to follow the steps both myself and Gruff gave you yet?
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #18  
Old 05-09-2011, 06:54 PM
henry0811 henry0811 is offline
Newcomer
 
Join Date: May 2011
Posts: 15
Default

Is this reading the file twice or do you mean my other button?

ioLine = ioFile.ReadToEnd
ioLines = ioLine
While Not ioLine = ""
ioLine = ioFile.ReadToEnd
ioLines = ioLines & vbCrLf & ioLine

I haven't made any attempt because I really don't get what you mean, even in the first part mate: "Simply call .Split on the ioFile variable passing in the correct line terminator (probably a CrLf pair) and assign it's result to a variable declared as an array of strings"

EDIT: Sorry mate I get you now, I have ioline = iofilereadtoend twice. Do I not need the While Not on my apply button then?
Reply With Quote
  #19  
Old 05-09-2011, 07:18 PM
henry0811 henry0811 is offline
Newcomer
 
Join Date: May 2011
Posts: 15
Default

I think I'm beat with this one mate. With no examples of exactly what I'm trying to achieve or anything and links to other sites I'm getting no where, and would be better off buying a book. I mean I've got this on my apply button...

Dim ioFile As New StreamReader("C:\Users\Gaming\Desktop\t.txt")

Dim ioLine As String ' Going to hold one line at a time
Dim ioLines As String ' Going to hold whole file
Dim arytext() As String


ioLine = ioFile.ReadToEnd

Now what? If you told me the next line, I could work the rest out for myself.
Reply With Quote
  #20  
Old 05-09-2011, 08:20 PM
henry0811 henry0811 is offline
Newcomer
 
Join Date: May 2011
Posts: 15
Default

Sorry guys, I was just a little frustrated with it

I've tried this but it doesn't seem to be doing much...

Dim ioFile As New StreamReader("C:\Users\Gaming\Desktop\t.txt")
Dim ioLines As String ' Going to hold whole file
Dim StringArray(2) As String

ioLines = ioFile.ReadToEnd

ioFile.Close()

StringArray = ioLines.Split("no" & vbLf & "yes")

For Each line In StringArray
If ComboBox9.Text = "Yes" Then
If line = "no" Then line = "yes"
ElseIf ComboBox9.Text = "no" Then
If line = "yes" Then line = "no"
End If
Next line
StringArray(2) = ioLines

Dim writer As New StreamWriter("C:\Users\Gaming\Desktop\t.txt")

writer.WriteLine(ioLines)
writer.Close()
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Struggling
Struggling
Struggling Struggling
Struggling
Struggling
Struggling Struggling Struggling Struggling Struggling Struggling Struggling
Struggling
Struggling
 
Struggling
Struggling
 
-->