Question Gevorg Arutiunian · Jun 13, 2017

Append a string to the beginning of a file

How do I append a string to the beginning of a %FileCharacterStream in an existing file?

Comments

Vitaliy Serdtsev · Jun 14, 2017

E.g.:

<FONT COLOR="#0000ff">s </FONT><FONT COLOR="#800000">fileName</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#008000">"C:\temp\test.txt"
</FONT><FONT COLOR="#0000ff">s </FONT><FONT COLOR="#800000">f</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#000080">##class</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008080">%FileCharacterStream</FONT><FONT COLOR="#000000">).</FONT><FONT COLOR="#0000ff">%New</FONT><FONT COLOR="#000000">()
</FONT><FONT COLOR="#0000ff">s </FONT><FONT COLOR="#800000">f</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Filename</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#800000">fileName
</FONT><FONT COLOR="#0000ff">s </FONT><FONT COLOR="#800000">f1</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#000080">##class</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008080">%FileCharacterStream</FONT><FONT COLOR="#000000">).</FONT><FONT COLOR="#0000ff">%New</FONT><FONT COLOR="#000000">()
</FONT><FONT COLOR="#0000ff">s </FONT><FONT COLOR="#800000">f1</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Filename</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#800000">fileName
   
</FONT><FONT COLOR="#0000ff">d </FONT><FONT COLOR="#800000">f</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Write</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008000">"start "</FONT><FONT COLOR="#000000">)
</FONT><FONT COLOR="#0000ff">d </FONT><FONT COLOR="#800000">f</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">CopyFrom</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">f1</FONT><FONT COLOR="#000000">)
</FONT><FONT COLOR="#0000ff">d </FONT><FONT COLOR="#800000">f</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Write</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008000">" end"</FONT><FONT COLOR="#000000">)

</FONT><FONT COLOR="#0000ff">d </FONT><FONT COLOR="#800000">f</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">SaveStream</FONT><FONT COLOR="#000000">() </FONT><FONT COLOR="#0000ff">s </FONT><FONT COLOR="#800000">f</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#008000">""</FONT><FONT COLOR="#000000">,</FONT><FONT COLOR="#800000">f1</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#008000">""</FONT>

Result:
test.txt (Before): bla-bla-bla
test.txt (After): start bla-bla-bla end
0
Rubens Silva  Jun 14, 2017 to Vitaliy Serdtsev

That's what I though, but it seemed slow.
Isn't there anyway to move the stream cursor back without erasing  the existing data?
And if not, couldn't that be done using a device directly?

0
John Murray  Jun 14, 2017 to Gevorg Arutiunian

Gevorg, you can give Vitaliy credit for the answer by clicking the checkmark alongside the answer header:

This will also mark your question as "answered".

0
Dmitry Maslennikov  Jun 14, 2017 to Rubens Silva

Not possible, due to data in the file stored sequentially. If you want to insert some data in any place, it means you should move all next data on the disk too. So, the only way it is a new file.

0