I am reading in a file, attempting to check if it is a binary file by checking the first n bytes for a NUL byte, and if it is not determined to be binary that way, it is manipulated as a string. I tried to loop over a string and check the first n indices for a NUL, but that would give false positives that checking a TBytes does not.
I use TFile.ReadAllBytes, which returns a TBytes and perform the NUL check on that. Then if no NUL is found, I use StringOf on the TBytes to get a string. I was wondering if StringOf has to make a copy of the data to make a string out of it (these are large files so I want to avoid that) and if so, what is a better way to do what I am trying to do.
Yes, according to the docs:
'Converts a byte array into a Unicode string using the default system locale.'If you just want to access the TBytes as a string, why not cast it to a PChar (if it’s Unicode) or PAnsiChar if it’s an AnsiString?
Example code:
EDIT
I’m a bit puzzled, why you’re not just using
TFile.OpenReadto get a FileStream.Let’s assume you’ve got gigabyte(s) of data and you’re in a hurry.
The Filestream will allow you to just read a small chunk of the data speeding things up.
This example code reads the whole file, but can easily be modified to only get a small part:
Note that the last example still reads all data from disk first (which may or may not be what your want).
However you can also read the data in chunks.
All of this is simpler with AnsiStrings because 1 char = 1 byte there :-).