Results 1 to 17 of 17

Thread: [RESOLVED] Reading a file in segments

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Resolved [RESOLVED] Reading a file in segments

    Greetings
    I need to read a file in segments for example
    if the file is 28 lines long
    I read first 4 lines then exit again opens the file read next four line and exit and so on
    I am using file system object
    The code is

    dim fs as new scripting.filesystemobject
    dim scr


    scr=fs.opentextfile("Path",forreading)
    do until scr.atendofstream
    scr.readline()
    loop

    scr.close
    scr=nothing


    this code reads the whole file line by line but I cannot figure out how to read line in segments equal or unequal

    Awaiting your reply and thnx in advance

  2. #2
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Reading a file in segments

    Quote Originally Posted by Abbas Haider
    Greetings
    I need to read a file in segments for example
    if the file is 28 lines long
    I read first 4 lines then exit again opens the file read next four line and exit and so on
    I am using file system object
    The code is

    dim fs as new scripting.filesystemobject
    dim scr


    scr=fs.opentextfile("Path",forreading)
    do until scr.atendofstream
    scr.readline()
    loop

    scr.close
    scr=nothing


    this code reads the whole file line by line but I cannot figure out how to read line in segments equal or unequal

    Awaiting your reply and thnx in advance
    Hi,

    See this link:

    http://www.vbforums.com/showthread.php?t=385258

    Its about reading a File and a way to read it Line by Line.

    Much succes,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reading a file in segments

    You should use a StreamReader to read text file. If you want to read four lines at a time you would normally use a loop and in that loop read four lines:
    VB Code:
    1. Do Until myStreamReader.Peek() = -1
    2.     myStreamReader.ReadLine()
    3.     myStreamReader.ReadLine()
    4.     myStreamReader.ReadLine()
    5.     myStreamReader.ReadLine()
    6. Loop
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: Reading a file in segments

    Jim
    Can you tell me how can I add streamreader class
    By some reference or something else

    since peek() throws exception here


    A.Haider

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reading a file in segments

    You have to create a StreamReader object first. It's a member of the System.IO namespace.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Hyperactive Member
    Join Date
    Mar 2005
    Location
    Bath, England
    Posts
    411

    Re: Reading a file in segments

    Quote Originally Posted by jmcilhinney
    You should use a StreamReader to read text file. If you want to read four lines at a time you would normally use a loop and in that loop read four lines:
    VB Code:
    1. Do Until myStreamReader.Peek() = -1
    2.     myStreamReader.ReadLine()
    3.     myStreamReader.ReadLine()
    4.     myStreamReader.ReadLine()
    5.     myStreamReader.ReadLine()
    6. Loop
    Jm made a mistake?!

    What happens if the number of lines in a file isn't exactly divisible by four? You'll get an EOF error at one of the following ReadLine statements, since you are only peeking ahead before the first one. Something like this would work:
    VB Code:
    1. While myStreamReader.Peek() > -1
    2.    For ii As Integer = 0 to 3
    3.       If myStreamReader.Peek() = -1 Then Exit While
    4.       myStreamReader.ReadLine()
    5.    Next ii
    6. End While
    Last edited by Parallax; Feb 5th, 2006 at 12:15 PM.
    "Make it idiot-proof and someone will make a better idiot"

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Reading a file in segments

    I'd go a bit further anyways. The whole management of the file opening and reading should be managed through a class. This is an excellent place for one. Have a class that takes the file name as an argument, then decide what you would want to do with the file. If you want to read the next four lines, have a member function of GetNextFourLines(). Leave the specific implementation of that up to the class.

    Of course, you would have to write that implementation, but as long as the GetNextFourLines() function worked, the specifics of the implementation are variable. One possibility would be to keep the file open so that the function only has to return the next four lines. Another option would be to close the file after each read, but retain in the class the last line read. This way, Jm's code could be slightly modifiied such that upon calling the function, the file would be opened, and lines would be read and discarded until the last line read, then the next four would be retained. Plenty of options.
    My usual boring signature: Nothing

  8. #8
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Reading a file in segments

    He didn't make a mistake If it wasn't divisible by four, then you probably wouldn't need to loop every four lines anyway, plus it was probably just a quick example.... Yes, it could catch an error, but if you were looping every four lines, then there would most likely be 4 lines for each block he needed to read (since the need was there in the first place). But nice catch Parallax

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: Reading a file in segments

    CIAO
    Jim
    My problem still persist I get exception even if I include the System.IO
    nullreferenceexception??? Why nullreferenceexception comes here

    If you have no idea about why it come then please tell me other way of reading the file in segments

    And yes I do not Hardcode it for four 4 lines. Got any other ideas for reading it in unequal segments?
    Last edited by Abbas Haider; Feb 5th, 2006 at 08:22 PM.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reading a file in segments

    Yes my first post was just a basic example based on assumptions. In a real world situation you would assign the lines read to some variable or the like as well. In actual fact, if you're expecting your text file to be grouped in blocks of four lines and that structure is not present then you shouldn't just exit the loop but you should in fact throw an exception if this is a method call or display an error message if it's the main app.

    To create a StreamReader you would do something like this:
    VB Code:
    1. Dim sr As New IO.StreamReader("place full path to file here")
    If you have imported the System.IO namespace then you can omit the 'OI.' part.

    As for specifically how to code this for exactly what you want, it depends on what the general case is. You have to have some structure around which to base your code. How are you determining how many lines you need to read at a time? Does this come from the user? Will it be constant through the entire file or will it be different at various points? You need to work out what the most general case is and then code that using variables. You can then assign values to those variables at run time to specialise the behaviour of the code based on user input or whatever.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: Reading a file in segments

    Still not working
    When I put the path as

    Dim s As System.IO.StreamReader("C:\readme.txt")

    It gives error "Array bounds cannot appear in type specifiers"

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: Reading a file in segments

    look
    I am using File system object

    Module Module1
    Dim fs As New Scripting.FileSystemObject
    Dim scr
    Dim s As System.IO.StreamReader("f:\readme.txt")
    Sub Main()

    scr = fs.OpenTextFile("c:\readme.txt", Scripting.IOMode.ForReading)
    Do Until s.Peek = -1
    (as per your method I was using Peek method My method was
    Do Until scr.Atendofstream)

    MsgBox(scr.readline)
    Loop
    scr.close()
    scr = Nothing

    End Sub
    End Module

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reading a file in segments

    The whole point was that you DON'T use the FileSystemObject. You just use a StreamReader. Also you have omitted the 'New' keyword. Finally there is no need to declare your variable at the module level as far as I can see. Obviously this section you're doing now is just a test, and the code for it should look something like this:
    VB Code:
    1. Sub Main()
    2.     'Open the file with a StreamReader.
    3.     Dim sr As [U]New[/U] IO.StreamReader("F:\readme.txt")
    4.  
    5.     'Read until there is no more text.
    6.     Do Until sr.Peek() = -1
    7.         'Read the next line and display it.  MessageBox.Show is preferable to MsgBox.
    8.         MessageBox.Show(sr.ReadLine())
    9.     Loop
    10.  
    11.     'Close the file.
    12.     sr.Close()
    13. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Exclamation Re: Reading a file in segments

    Greetings

    Thanks for your replies. However now the main target must be achieved
    as I stated I need to read a file in portions
    The file segments begin with "MESSAGE" and a segment is finished on "END"

    should I use the code
    do until mystreamreder.peek ="END"

    but then it comes the main part I cannot configure the logic to close the file after reading these lines then open it again to read after the lines which has been read previously


  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reading a file in segments

    You can't use Peek like that because it only returns the next character. You would need to leave the Do loop as it is but inside it you would read a line and check whether it was "END" and if so exit the loop. You cannot open a StreamReader at the same place you were at previously. You would have to use a variable to remember how many lines you have already read. Then when you open the file you read and discard that many lines, which puts you at the same place you left off last time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Reading a file in segments

    Or loop inside of your reader, for the "END" string
    VB Code:
    1. Dim Myreader As New System.io.StreamReader("c:\test.txt")
    2.         Dim MyBlock As String
    3.         Do While Myreader.Peek <> -1
    4.             Dim MyLine As String = Myreader.ReadLine
    5.             If MyLine = "END" Then
    6.                 MessageBox.Show(MyBlock)
    7.                 'Do something with your block
    8.                 MyBlock = ""  'clears myblock
    9.             Else
    10.                 MyBlock = MyBlock & MyLine & Environment.NewLine
    11.             End If
    12.         Loop
    13.         Myreader.Close()

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Posts
    275

    Re: Reading a file in segments

    It works great
    Even it has lesser code
    I have designed the mechanism in file system object but it has longer coding than this
    If any one one wants it I can give it to them

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width