Using "start" operator in createWriteStream

712 views
Skip to first unread message

axesh

unread,
Nov 17, 2011, 8:03:20 PM11/17/11
to nodejs
Does anybody have any example on using "start" parameter in
fs.createWriteStream method. Can anypoby post an example on how to use
the start parameter.The documentation suggests
the following

"may also include a start option to allow writing data at some
position past the beginning of the file".

So does this mean i can seek to any position in a file and then write
start inserting data from that position onwards. An example of what I
am trying to say is
Eg: Say i Have a file "tmp.txt" and it contain "Ho World!!!". Now if
i want to write "ell" suing writeStream method then how can it be
done..??

from nodes documentation it would seem like

var ws = fs.createWriteStream('tmp.txt',{flag:'r+',start:2}); //
Assumming the counter for begins from 1 and r+ flag since tmp is my
existing file and node documentation suggests that i use 'r+' for
exisint file

ws.end('ell','utf-8');

Now when i open the tmp.txt file it should contain 'Hello World!!!';

I plan to use strictly createWriteStream method so is my code valid.?
Is "start" parameter an accepted flag in createWriteStream method.

Any help would be really appreciated.

Thanks,
Axesh Ajmera


Thomas Shinnick

unread,
Nov 17, 2011, 8:57:09 PM11/17/11
to nod...@googlegroups.com
Documentation always has holes because of the obvious things the writer knows, that somehow don't get put _into_ the documentation.  :-(


On Thursday, November 17, 2011 7:03:20 PM UTC-6, axesh wrote:
Does anybody have any example on using "start" parameter in
fs.createWriteStream method. Can anypoby post an example on how to use
the start parameter.The documentation suggests
the following

 "may also include a start option to allow writing data at some
position past the beginning of the file".

Perhaps better would have been 
    "may also include a start option to allow over-writing data
     at some position past the beginning of the file"

Perhaps along with
    "Modifying a file rather than replacing it may require a flags
     mode of r+ rather than the default mode w."
changed to
    "Updating a file rather than truncating and discarding the entire file
     may require a flags mode of r+ rather than the default mode w."

So does this mean  i can seek to any position in a file and then write
start inserting data from that position onwards.

Not 'inserting', overwriting - replacing the previous contents with new data.  However many bytes of new data are written will replace previous bytes in the file. 

And of course if you are using UTF-8 with its variable number of bytes per _character_, that might not work correctly unless you are careful.
 
An example of what I
am trying to say is
Eg: Say i Have a file "tmp.txt"  and it contain "Ho World!!!". Now if
i want to write "ell" suing writeStream method then how can it be
done..??

from nodes documentation it would seem like

var ws = fs.createWriteStream('tmp.txt',{flag:'r+',start:2}); //
Assumming the counter for  begins from 1 and r+ flag since tmp is my
existing file and node documentation suggests that i use 'r+' for
exisint file

ws.end('ell','utf-8');

Now when i open the tmp.txt file it should contain 'Hello World!!!';

Much more likely would be "Hoellrld!!!"  First, because the beginning of the file is always at position 0, so position 2 would be where the space character was.  And as mentioned, writes replace (overwrite) old data.

I plan to use strictly createWriteStream  method so is my code valid.?
Is "start" parameter an accepted flag in createWriteStream method.

Any help would be really appreciated.

Thanks,
Axesh Ajmera

This feature is more likely useful with fixed format data, where you might want to replace a set of records with new records of the same size.  Or perhaps you want to append to an existing file.

Really, unless you know you need the feature (of setting the start write position) and why you need it, you don't want to use that feature.  A plain write stream, using createWriteStream without using that 'start' parameter, is what almost everybody actually wants.
Reply all
Reply to author
Forward
0 new messages