07-18-2018 03:39 PM - edited 07-18-2018 03:44 PM
When I use the attached code to write a text file 8 characters long (e.g. 12345678) and then overwrite it with 7 characters (e.g. abcdefg), the resulting file reads "abcdefg8". Also, I have noticed the size on disk is 0. I would expect it to be at the very least 7 characters.
In the larger project I am writing, I am reading the text file before I write to it (I am using the Set File Position function inbetween) so I don't want to 'Replace' the file when I open it.
How should I change my code so that the 8th character gets deleted when I overwrite with 7 characters and why is this happening?
Solved! Go to Solution.
07-18-2018 04:01 PM - edited 07-18-2018 04:03 PM
If you want to replace everything with the next text, you should open the file with "Replace or Create".
Alternatively you can Get File Position after the write, and use that to Set File Size.
But the former is simpler.
07-18-2018 04:06 PM
As I said (in my edit) in my larger project I am reading the file before I write to it. Do I really need to close, Replace and then write?
07-18-2018 04:11 PM
I did not see that when I first read your post. I may have had a copy open from before your edit. And since the snippet only showed open, write, close, that is what I was focused on.
You can do what I said in the second part of my post. (I edited it to add it about 2 minutes after I originally posted.) Just set the File size to match either the file position after the write, or the total number of characters in your string that you wrote out.
07-18-2018 04:57 PM
Thank you it works. Sorry about the edit.
I attached the working code.
07-18-2018 09:25 PM
Be sure to wire the error wire across those functions. That way if there is an issue with the first file open, it won't try to execute the following functions and throw errors on each and every one of them.
07-19-2018 11:50 AM
Yeah, I do that usually do that. Although if I'm just doing a test in the development environment, I sometimes leave it out and let the auto error handling take over. That way I can decide if I want the error ignored or not when it happens and it is easier to find the source of the error.