Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Reading Multi-Line data from a File seperately in .NET

vrao_nvrao_n Posts: 12Member
Hi,

I am trying to read data from a file by using StreamReader. When I use ReadLine statement it is allowing me to read a line only for once. But I need to read each line of file seperately and need to do some validations on that data.

Anybody plz help in resolving this...

Thanks in advance.

regards,
Venkat

Comments

  • kainsworthkainsworth Posts: 200Member
    Hi

    You can test each line at a time with code like this:
    [code]
    Dim strFileName As String = Directory.GetCurrentDirectory() & "MyFile.txt" ' pointer to file
    Dim txtFile As New FileStream(strFileName, FileMode.Open, FileAccess.Read)
    Dim FileStream As New StreamReader(txtFile)
    Dim strCheck As String ' Temp string for ReadLine

    Do Until FileStream.Peek = -1 ' Read until end of file
    strCheck = FileStream.ReadLine ' Next line into string
    ' Test
    If strCheck = Then
    Loop
    [/code]

    Did I understand your question correctly? If not, please do post a follow up and I'll try harder :-)

    Ged
  • vrao_nvrao_n Posts: 12Member
    Hi,

    You understand my need. But when I try to do so (with the code given), for first line it is reading data but from next line it is not.

    Thanks for your help, plz help me on this issue too.

    regards,
    Venkat.

    : Hi
    :
    : You can test each line at a time with code like this:
    : [code]
    : Dim strFileName As String = Directory.GetCurrentDirectory() & "MyFile.txt" ' pointer to file
    : Dim txtFile As New FileStream(strFileName, FileMode.Open, FileAccess.Read)
    : Dim FileStream As New StreamReader(txtFile)
    : Dim strCheck As String ' Temp string for ReadLine
    :
    : Do Until FileStream.Peek = -1 ' Read until end of file
    : strCheck = FileStream.ReadLine ' Next line into string
    : ' Test
    : If strCheck = Then
    : Loop
    : [/code]
    :
    : Did I understand your question correctly? If not, please do post a follow up and I'll try harder :-)
    :
    : Ged
    :

  • vrao_nvrao_n Posts: 12Member
    I am Sorry, it is the problem with file. There are blank rows in file, so it is giving an error.

    Thanks for valuable help ... Ged.

    : Hi,
    :
    : You understand my need. But when I try to do so (with the code given), for first line it is reading data but from next line it is not.
    :
    : Thanks for your help, plz help me on this issue too.
    :
    : regards,
    : Venkat.
    :
    : : Hi
    : :
    : : You can test each line at a time with code like this:
    : : [code]
    : : Dim strFileName As String = Directory.GetCurrentDirectory() & "MyFile.txt" ' pointer to file
    : : Dim txtFile As New FileStream(strFileName, FileMode.Open, FileAccess.Read)
    : : Dim FileStream As New StreamReader(txtFile)
    : : Dim strCheck As String ' Temp string for ReadLine
    : :
    : : Do Until FileStream.Peek = -1 ' Read until end of file
    : : strCheck = FileStream.ReadLine ' Next line into string
    : : ' Test
    : : If strCheck = Then
    : : Loop
    : : [/code]
    : :
    : : Did I understand your question correctly? If not, please do post a follow up and I'll try harder :-)
    : :
    : : Ged
    : :
    :
    :

  • kainsworthkainsworth Posts: 200Member
    Did you resolve the blank lines problem? If not, then something like this should do the trick:
    [code]
    ' Replace
    If strCheck = Then
    ' with
    If strCheck <> "" AND strCheck = Then
    [/code]

    Ged

    ================================================================



    : I am Sorry, it is the problem with file. There are blank rows in file, so it is giving an error.
    :
    : Thanks for valuable help ... Ged.
    :
    : : Hi,
    : :
    : : You understand my need. But when I try to do so (with the code given), for first line it is reading data but from next line it is not.
    : :
    : : Thanks for your help, plz help me on this issue too.
    : :
    : : regards,
    : : Venkat.
    : :
    : : : Hi
    : : :
    : : : You can test each line at a time with code like this:
    : : : [code]
    : : : Dim strFileName As String = Directory.GetCurrentDirectory() & "MyFile.txt" ' pointer to file
    : : : Dim txtFile As New FileStream(strFileName, FileMode.Open, FileAccess.Read)
    : : : Dim FileStream As New StreamReader(txtFile)
    : : : Dim strCheck As String ' Temp string for ReadLine
    : : :
    : : : Do Until FileStream.Peek = -1 ' Read until end of file
    : : : strCheck = FileStream.ReadLine ' Next line into string
    : : : ' Test
    : : : If strCheck = Then
    : : : Loop
    : : : [/code]
    : : :
    : : : Did I understand your question correctly? If not, please do post a follow up and I'll try harder :-)
    : : :
    : : : Ged
    : : :
    : :
    : :
    :
    :

Sign In or Register to comment.