There is such a code:

var Content: string; begin Content := 'Привет'; TFile.WriteAllText(OutFile, Content); Content := TFile.ReadAllText(OutFile); end; 

as a result, there is a gibber in Content .

How to work with these functions correctly, if instead of Content is data from another file, for example?

    1 answer 1

    In general, there are a lot of problems with TFile, if possible - you should not use it.

    The version of WriteAllText that you WriteAllText - writes to the file using the UTF8 encoding (which is good), but does not write to the BOM that there is a bad thing.

    As a result, ReadAllText tries to guess the encoding (since there is no BOM) and considers it ANSI (it goes by default). If only English characters are present, then there are no problems, but other languages ​​will really give cracks.

    The way out: specify the encoding explicitly either when writing or reading:

     // при записи: File.WriteAllText(OutFile, Content, TEncoding.UTF8); // в этом случае BOM будет записан и использован при ReadAllText // или при чтении: Content := TFile.ReadAllText(OutFile, TEncoding.UTF8); 
    • What then more or less adequate alternative? Via TStringStream or AssignFile? It turns out that I need to know what encoding my file is in? - gregor
    • @gregor, if the file does not contain a BOM - you need to know the encoding, because many (especially single-byte) to distinguish from each other is extremely difficult. The question of determining the file encoding does not have a unique solution. AssignFile - IMHO, try to forget about it. Taking into account the fact that you load the contents of a file completely into memory, I don’t think that the file size is over 100Mb (it’s just inappropriate to load this size into memory). I am a supporter of the use of a warm lamp TStringList, since he also has the ability to set an encoding. But the solution I proposed also has a place to be. - kami
    • 2
      @gregor if the contents of the file is encrypted - you cannot work with it as with text, because the encrypted information may contain (in terms of encoding) incorrect / invalid byte sequences, which will result in EEncodingError (or-as-it-there). It is necessary to work with binary content, the benefit of DCPCrypt allow it. Yes, TStringList loads everything into memory. But your code does it too, that's why I said с учетом того, что вы грузите содержимое файла полностью в память - kami
    • one
      @gregor, as you can see - detailing the original problem in the comments leads to a completely different solution. In your case, it would be correct to do this: upload text to a stream ( TStream successor) -> encrypt stream-> TStream to TStream > upload file to stream-> decrypt stream-> write to something text. - kami
    • one
      @gregor And with the encrypted data otherwise nothing. The alternative is AnsiString, RawByteString (you can cram anything into them, the main thing is to understand that this is not a string in the literal sense, but a set of bytes), TBytes. For the future - write questions in relation to the real problem, because with this turned out the problem of XYZ. - kami