file reading/updating/positioning nuances
jasonsikes opened this issue · 1 comments
I'm not exactly certain that this is an error. There are a lot of steps involved, and I don't know where to isolate the issue or if I have misinterpreted the intended actions. I am attempting to:
- create a file
- write a line to it
- close it
- reopen as update
- modify a small portion of the line (change "this" to "that")
- read the whole line with its modifications
I expected to read the line with its modifications. What I got instead was the original line without its modifications.
make "f "TestFileIO6.txt
openwrite :f
setwrite :f
print [this was another test]
closeall
openupdate :f
setwrite :f
setwritepos 2
type "at
setread :f
setreadpos 0
make "a readrawline
closeall
erf :f
print :a
EXPECTED: ---------------------------------------------
that was another test
OUTPUT: -----------------------------------------------
this was another test
I can replicate this behavior and, if I look in the file before deleting it, the at
string is appended to the end like so:
this was another test
at
Looking at the method, it is opening the file in append mode:
NODE *lopenupdate(NODE *arg) {
return(lopen(arg,"a+"));
}
The documentation I'm finding on fopen
indicates that append modes will always write to the end of the file regardless of position changes prior to the write.
If I change the mode to "r+"
, the behavior changes to allow writes to occur based on position setting. However, this comes with the side effect of changing the behavior if the file doesn't exist prior to the call. The "a+"
mode will allow opening a non-existent file while "r+"
does not.