Click here to Skip to main content
15,889,826 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello please i am trying to save the result of an operation where i modified some text in a sample application. The problem is i am having difficulty in trying to loop through all the data.Trying to print out my result to a file, i get only the last line of the file.My code simply refuse to loop and write all the data into a text file but it just writes the last data on the line.Please any help on how i can make this work will be highly appreciated.Once again thanks to all.Below is my code.

VB
Using myreader As StreamReader = New StreamReader("C:\Users\Jamiebones\Desktop\ConsoleApplication1\ConsoleApplication1\PHONE NUMBERS.txt")

          While Not (myreader.EndOfStream)


              Dim line As String = myreader.ReadLine
              Dim strBuilder As New Text.StringBuilder
              If String.IsNullOrEmpty(line) = False AndAlso line.Length > 1 Then
                  strBuilder.Append("234")
                  strBuilder.Append(line.Remove(0, 1))

                  For i = 0 To strBuilder.Length - 1

                      Dim writer As StreamWriter = New StreamWriter("C:\phone numbers.txt")
                      writer.Write(strBuilder.ToString)

                      writer.Close()

                  Next i

              End If

              Console.WriteLine(strBuilder.ToString)

          End While
          Console.ReadLine()

      End Using
Posted

This code is working

VB
Using myreader As StreamReader = New StreamReader("C:\Users\Jamiebones\Desktop\ConsoleApplication1\ConsoleApplication1\PHONE NUMBERS.txt")

            While Not (myreader.EndOfStream)


                Dim line As String = myreader.ReadLine
                Dim strBuilder As New Text.StringBuilder
                If String.IsNullOrEmpty(line) = False AndAlso line.Length > 1 Then
                    strBuilder.Append("234")
                    strBuilder.Append(line.Remove(0, 1))

                    For i = 0 To strBuilder.Length - 1

                        //Dim writer As StreamWriter = New StreamWriter("C:\phone numbers.txt")
                        Dim writer As StreamWriter = New StreamWriter("C:\phone numbers.txt", True)
                        writer.Write(strBuilder.ToString)

                        writer.Close()

                    Next i

                End If

                Console.WriteLine(strBuilder.ToString)

            End While
            Console.ReadLine()

        End Using
 
Share this answer
 
That is some very, very odd code - and it is difficult to work out exactly what to say here!
I am assuming that you want to read the strings from a file, and write them to another file, yes?
So why are you creating the file anew each time you read a line from the input file?
You have two loops, one inside the other:
while(not end of input)
   read a line
   for each character in the string
      create the file
      write a line to it


You don't need the inner loop - you need to move the content of the inner loop to three separate places :
1) Create the StreamWriter before the main loop.
2) Write the line inside the main loop
3) Close the file after the main loop.

Try this kind of thing with a pencil and paper - it can help to get the order of teh various processes sorted out in tyour mind before you rush into code.
 
Share this answer
 
Comments
jamiebones 24-Nov-12 2:08am    
Please am grateful for your help but can you help me with sample code on what you just highlighted I am still a novice at this.Trying to learn coming from a background not computer related.Thanks
OriginalGriff 24-Nov-12 2:36am    
No! :laugh:
This is your homework - it's only fair you should do some of it yourself!
It's not difficult: look at what I said, and identify where you are wrong. The only way I could give you code would be to re-write it so it was right, and that would not let you learn as well as doing it yourself.

(And trust me, learning at your current stage is a lot more important than you think - this is just a very simple exercise to make you start to think about how to do things, more than code)
jamiebones 24-Nov-12 3:25am    
Thanks i have been able to make it work because of your suggestion may God bless you.Please i want to ask for a favour from you.Please can you point me towards the best way on learning visual basic.I am teaching myself.I have a degree in pure and applied chemistry, but been always interested in computers.Any help you can render me will be very nice.
OriginalGriff 24-Nov-12 3:38am    
Get a book! Or a course - don't try to learn it by just attempting to write code - you will end up able to procuse applications, but you will miss so much important basic stuff that you will not be using the language effectively. Books and courses are structured to lead you through the basics, and to show how they are related as you move onto more complex stuff.

Almost any book will do, provided you avoid ones with "in xx Days", "for Dummies" or multiple exclamation marks in the title.
Pick one in your native language, and do all the exercises - they help to reinforce the information, making retention better.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900