StreamWriter writing some lines multiple times
I'm using StreamWriter to write project data into a file, but more often than not I'll get some lines written twice and maybe even three times. Is this a bug with my code or something else?
Code:
9 Replies
This is what I got the last time I ran the code:
And this is the expected output:
Are you overwriting a file? StreamWriter doesn't fulle overwrite the stream and only where you tell it. If the original file was longer, you'll have leftover lines
You can mark the end of the file by setting the length of the writer's
BaseStream
Note that the stream must be seekable for that but when creating the writer this way, the stream is, however it may not be the case if creating the writer from an existing stream.
Alternatively, you can write to a temp file and move it to the destination to replace the fileI am, but I'm clearing the file beforehand
I'm setting the file's length to zero
Even if there's no data in the file it produces output similar to this
im not seeing this kind of bug BUT
maybe this is not the code being executed
maybe it's not duplicating a line, it's the other stuff that is empty
maybe SceneList contains garbage
BUT ABOVE ALL
have you not debugged this?!
I've made sure that SceneList only contains proper data, and yes I've tried debugging. I have no clue what's going on, hence why I asked here.
I've tried seeing if anyone else is having such issues, I've tried giving the filesystem some time to sync changes, I've even tried .asking am entirely new program to test the stream writer (which worked just fine)
I'm pretty stumped
if you have debugged this then are at what point in PFWriter is sent something invalid? you can even set a breakpoint
To fully rule out filesystem issues, you can try creating the writer from a MemoryStream and convert the result to a string.
I'm going to try a different writer and see if anything changes, I forgot to do that during debugging
Something like this
Even using File.WriteAllLines has the same result
Maybe I'm doing something wrong
I've found the bug. It was a mix of the project compiling but refusing to write files to the disk for some reason, and (embarrassingly) SceneList contained some garbage because I wasn't correctly parsing project data files when opening them.
When I printed the contents of SceneList from a different class, I didn't see the garbage though.