How to work on TFileStream

Hello, I recently replaced TextFile with TFileStream . I never use it, so I have a little problem with it.

  • How can I add someting to my file after I assign it to a variable?
  • How can I read some form of this file?

I need a specific form line so that I do something like this:

 var linia_klienta:array[0..30] of string; AssignFile(tempPlik,'klienci.txt'); Reset(tempPlik); i:=0; While Not Eof(tempPlik) do begin Readln(tempPlik,linia_klient[i]); inc(i); end; CloseFile(tempPlik); 

Then when the second line is required, I just

 edit1.text = linia_klienta[1]; 
+6
source share
4 answers

If you need to read a text file and access each line, try using the TStringList class with this class instead, you can load the file, read the data (joining each line with an index) and save the data.

something like that

 FText : TStringList; i : integer; begin FText := TStringList.Create; try FText.LoadFromFile('C:\Foo\Foo.txt'); //read the lines for i:=0 to FText.Count-1 do ProcessLine(FText[i]); //do something //Add additional lines FText.Add('Adding a new line to the end'); FText.Add('Adding a new line to the end'); //Save the data back FText.SaveToFile('C:\Foo\Foo.txt'); finally FText.Free; end; end; end; 
+13
source

In newer versions of Delphi you can use TStreamReader / TStreamWriter here is an example of using TStreamReader ... this is only for managing text files

 var SR : TStreamReader; line : String; begin SR := TStreamReader.Create('D:\test.txt'); while not (SR.EndOfStream) do begin line := SR.ReadLine; ShowMessage(line); end; SR.Free; end; 
+6
source

TStream and its immediate descendants are basically a low-level access class. Mostly they work with generator buffers. There are several more specialized classes that descend or use a thread to perform tasks of a higher level.

Since Delphi 1 TReader and TWriter can be used to directly read and write Delphi types (inlcuding strings), but they were not designed to handle line-oriented files (unfortunately, they were designed too much for the properties of the components, not as a universal structure).

Turbo Power SysTools has the TStAnsiTextStream class, which implements linear access to text files in the same way as TextFile. Since the new Delphi 2009 classes (see opc0de's answer) provide the same access without using third-party libraries (in addition, they support different encodings due to support for Delphi 2009 codepage support, including Unicode).

+1
source

Depending on what you want to do, you need a stream class.

Do you want to work with text data (characters with dividing lines and end of line characters)?

OR, do you want to work with binary data?

I see that you are using a char array, not a string. Do you really want to use character data as if it were binary? Sometimes for some applications this case is required.

0
source

Source: https://habr.com/ru/post/903599/


All Articles