I asked some days ago this question in which people told me to use TFileStream. I am reading Głowacki's Expert Delphi and he uses TStreamReader/TStreamWriter. In a comment I have read this:
TStreamReader is a helper class for reading textual data. Your file is not text, it is binary. Use TFileStream directly instead
I'm a bit in trouble with this. I understand that a binary file does not contain text and to with TFileStream I can read binaries (as the documentation says). Let's say that I want to use TStreamWriter to output a series of numbers on a file and then read the content of this file. Am I allowed to do it with a TFileStream?
I see that TStreamWriter has the TEncoding in the constructor so I need to specify utf-8 or ansi for example. Does this affect the structure of the file?
I am new with this and I don't really understand the expert topics I've found online. From what I've understood TFileStream is for binary files and TStreamReader is for files that has some text inside
Not trying to confuse you, but in fact all files are binary.
They contain a number of bytes, and how these bytes are interpreted by humans or by software makes them useful.
But if the terms text and binary are opposed, then text means (more or less) anything that is human readable, like this answer, while binary means anything that is not text.
An example: in current Delphi versions, an integer is made up of four bytes. If you store these bytes directly to a file, you are writing binary. If you write the text form (e.g. -172423423) as characters, then you are writing text.
A TextWriter writes text to a stream. The TBinaryWriter writes data as it is stored in memory. Both can use any kind of stream, be it a TFileStream or a TMemoryStream to write to (or read from). The stream represents a file or some other writable medium.
Text, these days, can have many encodings, e.g. UTF-8, several Ansi codepages, etc. This means that "characters" that are not in the range #0..#127 are encoded differently. This can, of course, change the exact contents of the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With