Sign in with
Sign up | Sign in
Your question
Solved

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

Tags:
  • Programming
  • Apps
Last response: in Apps General Discussion
Share
May 28, 2012 11:05:50 AM

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.

More about : read file start half half end

May 29, 2012 11:05:17 AM

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
m
0
l
May 29, 2012 12:18:54 PM

mrfatbox said:
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



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)?
m
0
l
May 29, 2012 7:14:54 PM

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.
m
0
l
May 29, 2012 9:00:55 PM

houseinn2 said:
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)?



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...
m
0
l
a b L Programming
May 30, 2012 6:07:59 AM

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.
m
0
l

Best solution

May 30, 2012 8:12:57 AM

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
Share
June 8, 2012 3:00:41 AM

Thank you all.

sorry for the late reply.

Since, its going to take more time to read from backward.

I will be using 2 files normal way.

Thanks a lot.
m
0
l
June 8, 2012 3:01:08 AM

Best answer selected by houseinn2.
m
0
l
!