How to append data using just BinaryWriter at the end of the file?
-
20-06-2021 - |
Question
Maybe similar questions were asked in Stackoverflow, but I seem not to be able to find a solution to my problem.
I simply want to open a writable binary stream and be able to append data to the file using the same stream. Apparently, if you open a file for writing, it will erase all the data that it contained and start a new one. Plus, if you open a file for just reading, you can't write but only read. People did suggest online to use two separate stream - one for reading and the other for writing. I don't think that will work in this case.
Take a look at my sample code, I want to do something similar :
fs:BinaryWriter;
fs := new BinaryWriter(File.Create('c:\test.dat'));
fs.seek(0,SeekOrigin.End);
fs.Write('test string');
fs.Close;
La solution
If you open the writer like this
Stream fs = new FileStream(filename, FileMode.Append);
BinaryWriter bw = new BinaryWriter(fs);
it should open in append mode.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow