locked
Reading a Text file in VB.net RRS feed

  • Question

  • User351619809 posted

    Hi All,

     I am reading a text file in Vb.net. Below is my code.

     Dim reader As StreamReader
    
            'Read to the end of file Peek method return -1 if the end of file is reach
            Try
                reader = File.OpenText(fileInfo.FullName)
                While reader.Peek <> -1
    
                    line = reader.ReadLine()
    

     

    My text file has the following first line.

    @@@@This is a Test. Again this is a Test. reader.Readline() somehow skips the "@" sign and then starts reading from This is a Test...

    I am not sure why it is not reading @ sign.

    any help will be appreciated.

    Wednesday, July 15, 2015 12:03 PM

Answers

  • User475983607 posted

    I did a quick test and it worked fine.  Double check your file contents.

    Imports System
    Imports System.IO
    Imports System.Text
    
    Module Module1
    
        Sub Main()
            Dim path As String = "c:\temp\MyTest.txt"
    
            Try
                If File.Exists(path) Then
                    File.Delete(path)
                End If
    
                Dim sw As StreamWriter = New StreamWriter(path)
                sw.WriteLine("@@@@@This")
                sw.WriteLine("is some text")
                sw.WriteLine("to test")
                sw.WriteLine("Reading")
                sw.Close()
    
                Dim sr As StreamReader = New StreamReader(path)
    
                Do While sr.Peek() > -1
                    Console.WriteLine(sr.ReadLine())
                Loop
                sr.Close()
            Catch e As Exception
                Console.WriteLine("The process failed: {0}", e.ToString())
            End Try
        End Sub
    
    End Module

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 15, 2015 12:19 PM

All replies

  • User475983607 posted

    I did a quick test and it worked fine.  Double check your file contents.

    Imports System
    Imports System.IO
    Imports System.Text
    
    Module Module1
    
        Sub Main()
            Dim path As String = "c:\temp\MyTest.txt"
    
            Try
                If File.Exists(path) Then
                    File.Delete(path)
                End If
    
                Dim sw As StreamWriter = New StreamWriter(path)
                sw.WriteLine("@@@@@This")
                sw.WriteLine("is some text")
                sw.WriteLine("to test")
                sw.WriteLine("Reading")
                sw.Close()
    
                Dim sr As StreamReader = New StreamReader(path)
    
                Do While sr.Peek() > -1
                    Console.WriteLine(sr.ReadLine())
                Loop
                sr.Close()
            Catch e As Exception
                Console.WriteLine("The process failed: {0}", e.ToString())
            End Try
        End Sub
    
    End Module

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 15, 2015 12:19 PM
  • User351619809 posted

    I didn't look at the code properly. Someone was splitting at @ signs so they were not ending up in the table. I fixed the problem.

    Wednesday, July 15, 2015 1:54 PM
  • User-1601169661 posted

                    string line = null;
                    System.IO.TextReader readFile = new StreamReader("C:\\net-informations.txt");
                    line = readFile.ReadToEnd();
                    MessageBox.Show(line);
                    readFile.Close();
                    readFile = null;
                }
                catch (IOException ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

    More.....VB.Net Reader examples

     

    William

    Friday, August 14, 2015 2:20 AM