Hello I need to work with very big binary files so i can't use functions such as Assign(), Closefile() etc. I want to use TFileStream for its Read()/Write() methods. But I have problem because I can't read back what I've written to the stream. I have understood that the problem was related to the encoding so I think I need to use Unicode and not Ansi. But I haven't understood how to do it. Can someone help me, with examples ? I know how to write/read from/to a file but this doesn't help me solve the problem.
-
1`TFileStream` works with raw data (bytes), not strings, so your `Ansi` vs `Unicode` problem is by no means obvious or usual. You should include some code with your question. – Cosmin Prund Jun 10 '11 at 18:09
-
Open your `TFileStream` with `fmOpenReadWrite` or `fmCreate` and you can read as well as write. You also need to seek. Since you didn't give any code then it's hard to say more. – David Heffernan Jun 10 '11 at 19:11
3 Answers
If you control both writing and reading, you can use readers and writers. For example:
var
reader : TReader;
begin
reader := TReader.Create(MyStream, BufferSize);
try
myString : = reader.ReadString;
finally
reader.Free;
end;
end;
TReader has a way to distinguish between unicode and ansistring as long as they are written by TWriter.
If you don't controll the write part. You hopefully have a way to know the file format. (At least the strings and their size). So you can prepare a buffer to read the characters in.

- 52,876
- 38
- 145
- 202
Sorry, my answer was wrong, just like Andreas said. Maybe this post is going to help you? Writing a string to a TFileStream in Delphi 2010
-
1-1. Apparently you missed the first sentence of the Q: "i can't use function as Assign, Closefile etc". Also, you missed the title of the Q: "... **tfilestream** ". – Andreas Rejbrand Jun 10 '11 at 18:13
If you want to write/read strings from/to a stream, the easy way is to cast your string as a Shortstring which is always 255 char long (but it implies that your string must not be longer than 255 chars). If you only need to read/write strings in your streams then use a TStringList instead.
Otherwise you have other more complex options:
- write/ read a TStringList into the stream ( then you have to implement a kind of file format)
- write the string and to retrieve it read char by char and accumulate chars until a null char is encountered, repeat the operation for each string

- 1,988
- 13
- 27
-
1-1 for suggesting `ShortString`: way too many limitations associated with it to be used as a solution to this problem. +1 for suggesting `TStringList`. -1 for "kind of file format" with `TStringList`, because `TStringList` has `SaveToStream` and `LoadFromStream` methods, not to mention `SaveToFile` and `LoadFromFile`. -1 for suggesting reading and writing to stream char-by-char. – Cosmin Prund Jun 10 '11 at 18:46
-
About the file format: you can save the StringList to a TMemoryStream then you write the MemoryStream.Memory to the main stream. I use this a lot: in the main file you write the stream size then the stream.memory, this for an unlimited number of streams. Interpreting the file format is easy: read upcoming 'streamsize', cast next 'streamsize' bytes as something and so on... About the ShortString: limitations of this method were mentioned in the answer. – az01 Jun 10 '11 at 19:12
-
2I don't get why you would use a memory stream in between. Why not save the string list directly to the filestream? – jpfollenius Jun 10 '11 at 22:17