취소
다음에 대한 결과 표시 
다음에 대한 검색 
다음을 의미합니까? 

Overwrite Text File Old character left over

해결 완료!
솔루션으로 이동

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 포인트
1/7 메시지
3,618 조회수
솔루션
주제 작성자 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.

2/7 메시지
3,610 조회수

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 포인트
3/7 메시지
3,601 조회수

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 포인트
4/7 메시지
3,596 조회수

Thank you it works. Sorry about the edit.

 

I attached the working code.

0 포인트
5/7 메시지
3,575 조회수

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 포인트
6/7 메시지
3,561 조회수

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 포인트
7/7 메시지
3,529 조회수