LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Overwrite Text File Old character left over

Solved!
Go to solution

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?

0 Kudos
Message 1 of 7
(2,484 Views)
Solution
Accepted by topic author JonathanMATech

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.

Message 2 of 7
(2,476 Views)

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?

0 Kudos
Message 3 of 7
(2,467 Views)

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.

0 Kudos
Message 4 of 7
(2,462 Views)

Thank you it works. Sorry about the edit.

 

I attached the working code.

0 Kudos
Message 5 of 7
(2,441 Views)

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.

0 Kudos
Message 6 of 7
(2,427 Views)

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.

0 Kudos
Message 7 of 7
(2,395 Views)