TFileStream/fr: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: TFileStream/fr
| Old | New | Line | |
|---|---|---|---|
| 1 | + | # Description | |
| 2 | + | ||
| 3 | + | Un **TFileStream** est un descendant de [[TStream]] qui lit/écrit ses données depuis/vers un fichier sur disque. Un TFileStream charge l'entièreté des données d'un fichier en mémoire, et vice versa. | |
| 4 | + | ||
| 5 | + | # Constantes de création | |
| 6 | + | ||
| 7 | + | | Constante | Décimal | Description | | |
| 8 | + | |------------------|---------|--------------------------------------------------| | |
| 9 | + | | fmCreate | 65280 | Crée un nouveau fichier | | |
| 10 | + | | fmOpenRead | 0 | Ouvre un fichier en lecture | | |
| 11 | + | | fmOpenWrite | 1 | Ouvre un fichier en écriture | | |
| 12 | + | | fmOpenReadWrite | 2 | Ouvre un fichier en lecture/écriture | | |
| 13 | + | | fmShareDenyWrite | 32 | Empêche l'écriture si le fichier est déjà ouvert | | |
| 14 | + | ||
| 15 | + | # Lecture de données | |
| 16 | + | ||
| 17 | + | Lecture du fichier *fnam* : | |
| 18 | + | ||
| 19 | + | ``` pascal | |
| 20 | + | function readstream( fnam: string ): string; | |
| 21 | + | var | |
| 22 | + | strm: TFileStream; | |
| 23 | + | n: longint; | |
| 24 | + | txt: string; | |
| 25 | + | begin | |
| 26 | + | txt := ''; | |
| 27 | + | strm := TFileStream.Create( fnam, fmOpenRead or fmShareDenyWrite ); | |
| 28 | + | try | |
| 29 | + | n := strm.Size; | |
| 30 | + | SetLength( txt, n ); | |
| 31 | + | strm.Read( txt[1], n ); | |
| 32 | + | finally | |
| 33 | + | FreeAndNil( strm ); | |
| 34 | + | end; | |
| 35 | + | result := txt; | |
| 36 | + | end; | |
| 37 | + | ``` | |
| 38 | + | ||
| 39 | + | # Ecriture de données | |
| 40 | + | ||
| 41 | + | Ecriture de *txt* dans le fichier *fnam* : | |
| 42 | + | ||
| 43 | + | ``` pascal | |
| 44 | + | procedure writestream( fnam: string; txt: string ); | |
| 45 | + | var | |
| 46 | + | strm: TFileStream; | |
| 47 | + | n: longint; | |
| 48 | + | begin | |
| 49 | + | strm := TFileStream.Create( fnam, fmCreate ); | |
| 50 | + | n := Length( txt ); | |
| 51 | + | try | |
| 52 | + | strm.Position := 0; | |
| 53 | + | strm.Write( txt[1], n ); | |
| 54 | + | finally | |
| 55 | + | FreeAndNil( strm ); | |
| 56 | + | end; | |
| 57 | + | end; | |
| 58 | + | ``` | |
| 59 | + | ||
| 60 | + | # Voir aussi | |
| 61 | + | ||
| 62 | + | - [[doc:rtl/classes/tfilestream.html|Documentation de TFileStream]] | |
| 63 | + | - [[doc:rtl/classes/tstream.html|Documentation de TStream]] | |
| 64 | + | ||
| 65 | + | --- | |
| 66 | + | *From the Free Pascal wiki, page "TFileStream/fr", as it stood on 2020-03-01 after a change by Trev, 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).* |
