Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

The SilkTest methods FileWriteLine() and FileWriteValue() overwrite the current contents of the text file, how can information be inserted without this happening?

The SilkTest methods FileWriteLine() and FileWriteValue() overwrite the current contents of the text file, how can information be inserted without this happening?

 

For example, if the text file has the following text ;


111
222
333
444
555

A testcase is executed which will find 444 and add "44" to the end of it, by adding 2 characters to the end of the 444 line, the same amount is deleted from the 555 line. The following line is overwritten character by character as the script keeps writing to the file. Here we see what we expect the FileWriteLine or FileWriteValue() to result in, and what is actually performed.

Expected results
111
222
333
44444
555

Actual Results
111
222
333
44444
5

The behaviour outlined above is "as designed", the issue here is that the FileWriteLine() and FileWriteValue functions do not "insert".

An effective workaround is to read the file into a LIST OF STRING using ListRead(), modify the list, and write out the list to the same file using ListWrite().

For example:

[-] LIST OF STRING lsNewInfo = {...} 
    [ ] "*New line one*" 
    [ ] "*New line two*" 
    [ ] "*New line three*" 
[ ] LIST OF STRING lsFile 
[ ] INTEGER j 
[ ] 
[ ] ListRead (lsFile, "{GetProgramDir ()}\Sample.txt") 
[-] for j = 1 to ListCount (lsNewInfo) 
    [ ] ListInsert (lsFile, j, lsNewInfo) 
[ ] ListWrite (lsFile, "{GetProgramDir ()}\Sample.txt") 



Sample.txt before:

Line 1 
Line 2 
Line 3 
Line 4 
Line 5 


Sample.txt after:

*New line one* 
*New line two* 
*New line three* 
Line 1 
Line 2 
Line 3 
Line 4 
Line 5 


Old KB# 21753

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2013-02-15 19:23
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.