|
-
Feb 5th, 2006, 03:19 AM
#1
Thread Starter
Hyperactive Member
[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
-
Feb 5th, 2006, 03:26 AM
#2
Re: Reading a file in segments
 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
-
Feb 5th, 2006, 03:26 AM
#3
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:
Do Until myStreamReader.Peek() = -1
myStreamReader.ReadLine()
myStreamReader.ReadLine()
myStreamReader.ReadLine()
myStreamReader.ReadLine()
Loop
-
Feb 5th, 2006, 04:33 AM
#4
Thread Starter
Hyperactive Member
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
-
Feb 5th, 2006, 04:35 AM
#5
Re: Reading a file in segments
You have to create a StreamReader object first. It's a member of the System.IO namespace.
-
Feb 5th, 2006, 10:36 AM
#6
Hyperactive Member
Re: Reading a file in segments
 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:
Do Until myStreamReader.Peek() = -1
myStreamReader.ReadLine()
myStreamReader.ReadLine()
myStreamReader.ReadLine()
myStreamReader.ReadLine()
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:
While myStreamReader.Peek() > -1
For ii As Integer = 0 to 3
If myStreamReader.Peek() = -1 Then Exit While
myStreamReader.ReadLine()
Next ii
End While
Last edited by Parallax; Feb 5th, 2006 at 12:15 PM.
"Make it idiot-proof and someone will make a better idiot"
-
Feb 5th, 2006, 01:51 PM
#7
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
 
-
Feb 5th, 2006, 01:52 PM
#8
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
-
Feb 5th, 2006, 08:01 PM
#9
Thread Starter
Hyperactive Member
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.
-
Feb 5th, 2006, 08:19 PM
#10
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:
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.
-
Feb 5th, 2006, 08:27 PM
#11
Thread Starter
Hyperactive Member
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"
-
Feb 5th, 2006, 08:31 PM
#12
Thread Starter
Hyperactive Member
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
-
Feb 5th, 2006, 09:03 PM
#13
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:
Sub Main()
'Open the file with a StreamReader.
Dim sr As [U]New[/U] IO.StreamReader("F:\readme.txt")
'Read until there is no more text.
Do Until sr.Peek() = -1
'Read the next line and display it. MessageBox.Show is preferable to MsgBox.
MessageBox.Show(sr.ReadLine())
Loop
'Close the file.
sr.Close()
End Sub
-
Feb 10th, 2006, 06:00 PM
#14
Thread Starter
Hyperactive Member
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
-
Feb 10th, 2006, 06:45 PM
#15
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.
-
Feb 10th, 2006, 07:18 PM
#16
Re: Reading a file in segments
Or loop inside of your reader, for the "END" string
VB Code:
Dim Myreader As New System.io.StreamReader("c:\test.txt")
Dim MyBlock As String
Do While Myreader.Peek <> -1
Dim MyLine As String = Myreader.ReadLine
If MyLine = "END" Then
MessageBox.Show(MyBlock)
'Do something with your block
MyBlock = "" 'clears myblock
Else
MyBlock = MyBlock & MyLine & Environment.NewLine
End If
Loop
Myreader.Close()
-
Feb 25th, 2006, 01:12 PM
#17
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|