ofstream: Clasă stream pentru scrierea în fișiereifstream: Clasă stream pentru citirea din fișierefstream: Clasă stream petru operții de citire/scriere din/în fișiere.istream și ostream. Deja am folosit obiecte din aceste clase: cin este un obiect al clasei istream și cout este un obiect al clasei ostream. De aceea, noi deja am folosit clasele care conțin stream-uri (fluxuri) la fișiere. De fapt, putem folosi fluxurile pentru fișiere în aceeași manieră în care am folosit cin și cout, cu singura diferență că va trebui să asociem aceste fluxuri unor fișiere fizice. Să vedem un exemplu: |
|
[fișierul exemplu.txt] Scriem acest text intr-un fisier. |
exemplu.txt și inserează un test în el, în același fel cum eram obișnuiți cu cout, dar de data aceasta folosim fluxul fisierul_meu.fisierul_meu) și orice operație de intrare sau ieșire cu acest flux va fi aplicată fișierului fizic asociat.open:
open (nume_fișier, mod);
nume_fișier este un string reprezentănd numele fișierului ce trebuie deschis, iar mod este un parametru opțional conțnând una dintre combinațiile de mai jos:ios::in | Deschide pentru operații de citire. |
ios::out | Deschide pentru operații de scriere. |
ios::binary | Deschide în mod binar. |
ios::ate | Setează poziția inițială la sfârșitul fișierului. Dacă nu se precizează, poziția inițială este la începutul fișierului. |
ios::app | Toate operațiile de scriere se efectuează la sfârșitul fișierului, adăugându-se la cnținutul curent al fisierului. |
ios::trunc | Dacă fișierul este deschis pentru operații de ieșire și fișierul exista anterior, întregul său conținut este șters și va fi înlocuit cu noile informații. |
|). De exemplu, dacă vrem să deschidem fișierul exemplu.bin în mod binar pentru a adăuga informații, am putea-o face apelând următoarea funcție membru open: |
|
open ale claselor ofstream, ifstream și fstream are un mod implicit de lucru pe care îl folosește atunci când fișierul ete deschis fără al doilea parametru:| clasa | mod implicit |
|---|---|
ofstream | ios::out |
ifstream | ios::in |
fstream | ios::in | ios::out |
ifstream și ofstream, indicatorii folosiți automat sunt ios::in respectiv ios::out, chiar dacă se transmite funcției membru open un al parametru care nu îi include (indicatorii vor fi combinați).fstream, valoarea implicită se aplică numai dacă funcția se apelează fără parametrul mod. Dacă funcția este apelată cu o valoare pentru al doilea parametru, atunci modul implicit este suprascris, fără combinare.open , constructor care are exact aceeași parametri ca funcția membru. De aceea, am fi putut declara obiectul fisierul_meu din exemplul anterior scriind: |
|
is_open. Această funcție returnează valoarea true de tip bool în cazul în care obiectul stream este asociat unui fișier deschis, respectiv false în caz contrar: |
|
close a fluxului. Această funcție curăță buffer-ele asociate și închide fișierul: |
|
close.ios::binary în modul de deschidere. Aceste fișiere sunt proiectate pentru a stoca text și, de aceea, toate valorile care se citesc/scriu din/în ele pot suferi transformări care nu corespund neapărat valorilor lor binare exacte.cout: |
|
[fișierul exemplu.txt] Aceasta este o linie. Aceasta este alta linie. |
cin: |
|
Aceasta este o linie. Aceasta este alta linie. |
true dacă fluxul este pregătit pentru mai multe operații, respectiv are valaorea false fie când se ajunge la sfârșitul fișierului, fie când apare altă eroare.bool): bad()true dacă o operație de citire sau scriere eșuează. De exemplu, dacă încercăm să scriem într-un fișier care nu este deschis pentru scriere sau dacă nu mai este spațiu disponibil pentru scriere.fail()true în aceleași situații ca bad(), dar și atunci când apare o eroare de format, ca de exemplu la întâlnirea unui caracter alfabetic într-o operație care necesită citirea unui număr întreg.eof()true dacă s-a ajuns la sfârșitul unui fișier deschis pentru citire.good()false în aceleași situații în care apelul oricăreia dintre funcțiile anterioare ar returna true. Să observăm că funcțiile good și bad nu sunt chiar opuse (good verifică mai mulți indicatori de stare deodată).clear() poate fi folosită pentru a reseta indicatorii de stare.ifstream, like istream, keeps an internal get position with the location of the element to be read in the next input operation.ofstream, like ostream, keeps an internal put position with the location where the next element has to be written.fstream, keeps both, the get and the put position, like iostream.streampos, which is a type representing the current get position (in the case of tellg) or the put position (in the case of tellp).
seekg ( position );
seekp ( position );
position (counting from the beginning of the file). The type for this parameter is streampos, which is the same type as returned by functions tellg and tellp.
seekg ( offset, direction );
seekp ( offset, direction );
direction. offset is of type streamoff. And direction is of type seekdir, which is an enumerated type that determines the point from where offset is counted from, and that can take any of the following values:ios::beg | offset counted from the beginning of the stream |
ios::cur | offset counted from the current position |
ios::end | offset counted from the end of the stream |
|
|
size is: 40 bytes. |
begin and end: |
|
streampos is a specific type used for buffer and file positioning and is the type returned by file.tellg(). Values of this type can safely be subtracted from other values of the same type, and can also be converted to an integer type large enough to contain the size of the file.streampos and streamoff. These types are also defined as member types of the stream class:| Type | Member type | Description |
|---|---|---|
streampos | ios::pos_type | Defined as fpos<mbstate_t>.It can be converted to/from streamoff and can be added or subtracted values of these types. |
streamoff | ios::off_type | It is an alias of one of the fundamental integral types (such as int or long long). |
<< and >>) and functions like getline is not efficient, since we do not need to format any data and data is likely not formatted in lines.write and read. The first one (write) is a member function of ostream (inherited by ofstream). And read is a member function of istream (inherited by ifstream). Objects of class fstream have both. Their prototypes are:memory_block is of type char* (pointer to char), and represents the address of an array of bytes where the read data elements are stored or from where the data elements to be written are taken. The size parameter is an integer value that specifies the number of characters to be read or written from/to the memory block. |
|
the entire file content is in memory |
ios::ate flag, which means that the get pointer will be positioned at the end of the file. This way, when we call to member tellg(), we will directly obtain the size of the file. |
|
|
|
streambuf. This buffer object may represent a memory block that acts as an intermediary between the stream and the physical file. For example, with an ofstream, each time the member function put (which writes a single character) is called, the character may be inserted in this intermediate buffer instead of being written directly to the physical file with which the stream is associated.flush and endl.sync() causes an immediate synchronization. This function returns an int value equal to -1 if the stream has no associated buffer or in case of failure. Otherwise (if the stream buffer was successfully synchronized) it returns 0.Preprocessor directives | Index |