TFileStream: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: TFileStream
| Old | New | Line | |
|---|---|---|---|
| 1 | + | A **TFileStream** is a descendant of [[TStream]] that gets/stores its data from/to a file on disk. | |
| 2 | + | ||
| 3 | + | | Constant | Decimal | Description | | |
| 4 | + | |------------------|---------|--------------------------------------------| | |
| 5 | + | | fmCreate | 65280 | Creates a new file | | |
| 6 | + | | fmOpenRead | 0 | opens a file for reading | | |
| 7 | + | | fmOpenWrite | 1 | opens a file for writing | | |
| 8 | + | | fmOpenReadWrite | 2 | opens a file for reading and writing | | |
| 9 | + | | fmShareDenyWrite | 32 | prohibit writing if file is already opened | | |
| 10 | + | ||
| 11 | + | - read entire file *fnam* into a string. | |
| 12 | + | ||
| 13 | + | ``` pascal | |
| 14 | + | function readstream(fnam: string): string; | |
| 15 | + | var | |
| 16 | + | strm: TFileStream; | |
| 17 | + | n: longint; | |
| 18 | + | txt: string; | |
| 19 | + | begin | |
| 20 | + | txt := ''; | |
| 21 | + | strm := TFileStream.Create(fnam, fmOpenRead or fmShareDenyWrite); | |
| 22 | + | try | |
| 23 | + | n := strm.Size; | |
| 24 | + | SetLength(txt, n); | |
| 25 | + | strm.Read(txt[1], n); | |
| 26 | + | finally | |
| 27 | + | strm.Free; | |
| 28 | + | end; | |
| 29 | + | result := txt; | |
| 30 | + | end; | |
| 31 | + | ``` | |
| 32 | + | ||
| 33 | + | - read 8 bytes of *fnam* into a byte array: | |
| 34 | + | ||
| 35 | + | ``` pascal | |
| 36 | + | .. | |
| 37 | + | type | |
| 38 | + | TByte8Array = array [0..7] of byte; | |
| 39 | + | .. | |
| 40 | + | function readstream(fnam: string; position= integer): TByte8Array; | |
| 41 | + | var | |
| 42 | + | strm : TFileStream; | |
| 43 | + | begin | |
| 44 | + | strm := TFileStream.Create(fnam, fmOpenRead or fmShareDenyWrite); | |
| 45 | + | try | |
| 46 | + | strm.Position:=position; | |
| 47 | + | strm.Read (Result[0],8); | |
| 48 | + | finally | |
| 49 | + | strm.Free; | |
| 50 | + | end; | |
| 51 | + | end; | |
| 52 | + | ``` | |
| 53 | + | ||
| 54 | + | - Write a string *txt* to the file *fnam*. | |
| 55 | + | ||
| 56 | + | ``` pascal | |
| 57 | + | procedure writestream(fnam: string; txt: string); | |
| 58 | + | var | |
| 59 | + | strm: TFileStream; | |
| 60 | + | n: longint; | |
| 61 | + | begin | |
| 62 | + | strm := TFileStream.Create(fnam, fmCreate); | |
| 63 | + | n := Length(txt); | |
| 64 | + | try | |
| 65 | + | strm.Position := 0; | |
| 66 | + | strm.Write(txt[1], n); | |
| 67 | + | finally | |
| 68 | + | strm.Free; | |
| 69 | + | end; | |
| 70 | + | end; | |
| 71 | + | ``` | |
| 72 | + | ||
| 73 | + | ## See also | |
| 74 | + | ||
| 75 | + | - [[File types]] | |
| 76 | + | - [[doc:rtl/classes/tfilestream.html|TFileStream doc]] | |
| 77 | + | - [[doc:rtl/classes/tstream.html|TStream doc]] | |
| 78 | + | ||
| 79 | + | --- | |
| 80 | + | *From the Free Pascal wiki, page "TFileStream", as it stood on 2022-04-12 after a change by Bart, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
