Write data in a file....

  • Thread starter Thread starter rr
  • Start date Start date
R

rr

Hi,

i have to write a file composed of n packets of a given length.
I need to write this file writing every packet when i get it, knowing its
number and length.
I mean:
packet #0 -length 100 starting from position 1
packet #5 -length 100 starting from position 501
packet #3 -length 100 starting from position 301 ......and so on.

and when the last packet come write down to hard disk.

Please any example on how to write data to a given address in a file?Is it
better to use a filestream or first memorystream and record the file when
the memorystream is full?Mmmmh little confused......:-)
Thanks for your help.
rr
 
rr said:
i have to write a file composed of n packets of a given length.
I need to write this file writing every packet when i get it, knowing its
number and length.
I mean:
packet #0 -length 100 starting from position 1
packet #5 -length 100 starting from position 501
packet #3 -length 100 starting from position 301 ......and so on.

and when the last packet come write down to hard disk.

Please any example on how to write data to a given address in a file?Is it
better to use a filestream or first memorystream and record the file when
the memorystream is full?Mmmmh little confused......:-)

You can use the Position property (or call Seek) to get to the right
place, calling FileStream.SetLength if necessary first to make sure the
file has enough space.
 
I am having a problem with FileStream.Position property.
Here is the scenario:

1) I create a FileStream around a text-file with <NEWLINE> as field
delimitter.
2) I create a StreamReader around this FileStream.
3) In a while loop, I start reading line by line from the StreamReader.
4) I am looking for a specific string that marks the beginning of the
text-block I am
interested in and another text-string that marks the end of the block I am
interested in.
5) Currently, I am doing this using the Position property of the underlying
filestream. This does not seem to be reliable.

The first line is 34 characters, and after reading the first line my
position is 1024.
I will appreciate any pointers on this issue. Can't seem to find any
documentation on this.

Thank you!
 
Back
Top