[vb] Is it possible to read a file as (start-half) and (half-end)?

houseinn2

Honorable
Mar 14, 2012
4
0
10,510
I am using vb 2005.
I am trying to write a code.

Used two files, with two streamreader.normal way.

Wonder if it is possible to a read file such that:
first streamreader reads from start to half
and
second streamreader reades from half to end?

any help is appreciated.
 
Solution
i cant add any more then what has been said but will say what i was going to anyway

if you have to read the file to get the number of lines then you have read the file once already and enen if 2 streamreaders could read the file 2x faster the first read has canceled it out.
and even if you could start at line 50 then program has to read the first 49 lines to find line 50

mrfatbox

Distinguished
Sep 22, 2011
7
0
18,520
ok 3 things
1) the problem i can see with this is that you would first need to read the compleat file to be able to set the middle or half way as you put it.
2) do you want to read the last part of the file backward as starting from the end working to the middle will read the lines in in the wrong order
3)why do you want to read the file in 2 parts. are you trying to read the file faster
 

houseinn2

Honorable
Mar 14, 2012
4
0
10,510



Thank you for the reply,mrfatbox.

Yes, I want to read the file as faster as I can.

Once I read the total file, and i can find the total lines
eg. number of lines 100.

then, I know 50 is the middle part.

In first systemreader if I read from 1-50,how can second systemreader read the file same file from 51-100
or as you said in 2) how can I read from backward(100-51)?
 

mgwildi

Honorable
Apr 20, 2012
8
0
10,510
hullo,

hmmm... a FileStream object gives you a Length property, telling you the number of bytes in the file...

You could try opening a second FileStream object on the same file (make sure they are set with a Read "file share") and set its Position property to Length / 2 (rounded up?) ... oh eh this would work only if all your lines have the same number of bytes (but there's probably something you can do to get your middle line position...)

Then I'd start two threads, one for each filestream, one starting at position 0 up to your "middle position", the other starting in the middle end ending at the EOF

I rapidly tried it, but didn't get it to work, but it seemed doable.
 

theDanijel

Distinguished
May 4, 2011
74
0
18,590



By doing that you are takeing more time to read it. Firstm you are doing here 2 or more reads throu all the file. Second you can't have 2 or more readers on the same file (at least without closing one beafore instancing the other)! Third, using multiple readers makes no sense unless they are running in seperate threads.
Just use ReadToEnd() method. It will read it very fast and then you can use that data any way you want...
 

randomizer

Distinguished
As pointed out, this probably end up slower than just reading it once. There is no reason to read multiple parts of a sequential file simultaneously as you'll just thrash your hard disk. If the file is very large and the processing of each line is computationally intensive then read a batch of lines (if they are short lines then you cam read thousands of them; if they are long then read fewer), pass them to another thread to process and read the next lot. If each line takes only a short amount of time to process then you can do it all with one thread. If the file is relatively short (ie. not millions of lines) then you won't need to read it in batches. This is mostly important to avoid running out of memory with large files.
 

mrfatbox

Distinguished
Sep 22, 2011
7
0
18,520
i cant add any more then what has been said but will say what i was going to anyway

if you have to read the file to get the number of lines then you have read the file once already and enen if 2 streamreaders could read the file 2x faster the first read has canceled it out.
and even if you could start at line 50 then program has to read the first 49 lines to find line 50
 
Solution