Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET IO strings
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.
 
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 23-Nov-12 15:40pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This code is working
 
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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
jamiebones at 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 at 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 at 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 at 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)

  Print Answers RSS
0 Marcin Kozub 310
1 OriginalGriff 248
2 Sergey Alexandrovich Kryukov 215
3 Richard MacCutchan 202
4 Praneet Nadkar 197
0 OriginalGriff 8,048
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 24 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100