Last active
March 10, 2024 22:30
-
-
Save ovidiudeica/a26bc58faac8295ae3529f8dfb9502fd to your computer and use it in GitHub Desktop.
Algoritmi cu fisiere in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// suma a 2 numere in fisier | |
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
int main() | |
{ | |
int a, b; | |
// deschide fisierul pentru scriere: | |
ofstream f("suma.txt"); | |
// sau fstream f("suma.txt, ios::out"); | |
// citeste de la tastatura 2 numere: | |
cout << "Introduceti 2 numere: \n"; | |
cout << "a = "; cin >> a; | |
cout << "b = "; cin >> b; | |
// scrie numerele in fisier: | |
f << "a = " << a << " " << "b = " << b << endl; | |
// scrie suma in fisier: | |
f << "Suma este: " << a + b << endl; | |
// inchide fisierul: | |
f.close(); | |
// se termina programul: | |
cout << "Gata! Vizualizati fisierul 'suma.txt'!\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Citeste de la tastatura 2 numere si le scrie in fisierul "suma.txt", iar pe linia urmatoare suma lor. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// produsul a 2 numere din fisier | |
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
int main() | |
{ | |
long a, b; | |
// deschide fisierul pentru citire: | |
ifstream f("numere.txt"); | |
// sau fstream ("numere.txt", ios::in); | |
// se testeaza daca fisierul poate fi deschis: | |
if (!f) | |
{ | |
// afiseaza un mesaj: | |
cout << "Eroare la deschiderea fisierului.\n"; | |
// se paraseste programul: | |
return 0; | |
} | |
// citeste numerele din fisier: | |
f >> a >> b; | |
// inchide fisierul: | |
f.close(); | |
// afiseaza produsul numerelor: | |
cout << "Produsul este: " << a * b << endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Citeste din fisierul "numere.txt" 2 numere intregi, separate printr-un spatiu si afiseaza produsul acestora pe ecran. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// media aritmetica a 3 numere in fisier | |
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
int main() | |
{ | |
long a, b, c; | |
// deschide fisierul pentru citire si dupa pentru adaugare: | |
fstream f("numere3.txt", ios::in | ios::app); | |
// se testeaza daca fisierul poate fi deschis: | |
if (!f) | |
{ | |
// afiseaza un mesaj: | |
cout << "Eroare la deschiderea fisierului.\n"; | |
// se paraseste programul: | |
return 0; | |
} | |
// citeste numerele din fisier: | |
f >> a >> b >> c; | |
f << endl; | |
// calculeza si adauga media in fisier: | |
f << "Media este: " << (float)(a + b + c) / 3; | |
// inchide fisierul: | |
f.close(); | |
// se termina programul: | |
cout << "Gata! Vizualizati fisierul 'numere3.txt'!\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Citeste din fisierul "numere3.txt" 3 valori intregi, pe linii diferite si calculeaza media lor aritmetica, iar aceasta va fi | |
adaugata in fisier. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// citire - afisare vector din fisier | |
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
int main() | |
{ | |
long x[101]; | |
int n, i; | |
// deschide fisierul pentru citire: | |
ifstream f("vector.txt"); | |
// sau fstream ("vector.txt", ios::in); | |
// se testeaza daca fisierul poate fi deschis: | |
if (!f) | |
{ | |
// afiseaza un mesaj: | |
cout << "Eroare la deschiderea fisierului.\n"; | |
// se paraseste programul: | |
return 0; | |
} | |
// se citeste valoarea lui n: | |
f >> n; | |
// se citesc numerele in vectorul x: | |
for (i = 1; i <= n; i++) | |
f >> x[i]; | |
// inchide fisierul: | |
f.close(); | |
// se afiseaza numerele din fisier pe monitor: | |
cout << "Numerele din fisier sunt: "; | |
for (i = 1; i <= n; i++) | |
cout << x[i] << " "; | |
// trece pe linia urmatoare: | |
cout << endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Citeste din fisierul "vector.txt" numerele (maxim 100) intr-o variabila de tip tablou unidimensional (vector) si afiseaza | |
numerele citite pe monitor. | |
In fisierul "vector.txt", informatia este structurata astfel: | |
- pe prima linie o valoare naturala ce reprezinta numarul de numere (n); | |
- pe urmatoarele n linii numerele. | |
Rezolvare: | |
- se citeste de pe prima linie numarul de numere in variabila n; | |
- intr-o instructiune for, se citesc numerele in vector. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// citire - afisare vector din fisier | |
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
int main() | |
{ | |
int x[101], n, a, i; | |
// deschide fisierul pentru citire: | |
ifstream f("vector1.txt"); | |
// sau fstream ("vector1.txt", ios::in); | |
// se testeaza daca fisierul poate fi deschis: | |
if (!f) | |
{ | |
// afiseaza un mesaj: | |
cout << "Eroare la deschiderea fisierului.\n"; | |
// se paraseste programul: | |
return 0; | |
} | |
n = 0; | |
f >> a; | |
// citeste numerele din fisier: | |
while (!f.eof()) | |
{ | |
n++; | |
x[n] = a; | |
// sau x[++n] = a; | |
f >> a; | |
} | |
// inchide fisierul: | |
f.close(); | |
// afiseaza numerele din fisier pe monitor: | |
cout << "Numerele din fisier sunt: "; | |
for (i = 1; i <= n; i++) | |
cout << x[i] << " "; | |
// trece pe linia urmatoare: | |
cout << endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Citeste din fisierul "vector1.txt" numerele (maxim 100) intr-o variabila de tip tablou unidimensional (vector) si afiseaza | |
numerele citite pe monitor. | |
In fisierul "vector1.txt", informatia este structurata astfel: | |
- pe prima linie numerele despartite de cate un spatiu; | |
- nu se cunoaste cate numere sunt in fisier. | |
Rezolvare: Deoarece nu se cunoaste numarul de numere din fisier, se citesc pe rand numerele intr-o instructiune while cat timp | |
mai sunt numere in fisier (se testeaza sfarsitul de fisier). Numerele citite vor fi memorate in vectorul x. La fiecare citire | |
se incrementeaza variabila n, la sfarsit aceasta va retine numarul de numere din fisier. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// citire - afisare vector in fisier | |
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
int main() | |
{ | |
int x[101], n, a, i; | |
// deschide fisierul 'vector1.txt' pentru citire: | |
ifstream f("vector1.txt"); | |
// sau fstream ("vector1.txt", ios::in); | |
// testeaza daca fisierul 'vector1.txt' poate fi deschis: | |
if (!f) | |
{ | |
// afiseaza un mesaj: | |
cout << "Eroare la deschiderea fisierului.\n"; | |
// se paraseste programul: | |
return 0; | |
} | |
// deschide fisierul 'vector2.txt' pentru scriere: | |
ofstream g("vector2.txt"); | |
// sau fstream ("vector2.txt", ios::out); | |
n = 0; | |
f >> a; | |
// citeste numerele din fisierul 'vector1.txt': | |
while (!f.eof()) | |
{ | |
n++; | |
x[n] = a; | |
// sau x[++n] = a; | |
f >> a; | |
} | |
// inchide fisierul 'vector1.txt': | |
f.close(); | |
// afiseaza numerele in fisierul 'vector2.txt': | |
g << "Numerele din fisier sunt: "; | |
for (i = 1; i <= n; i++) | |
g << x[i] << " "; | |
// inchide fisierul 'vector2.txt': | |
g.close(); | |
// se termina programul: | |
cout << "Gata! Vizualizati fisierul 'vector2.txt'!\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Citeste din fisierul "vector1.txt" numerele (maxim 100) intr-o variabila de tip tablou unidimensional (vector) si afiseaza | |
numerele citite in fisierul "vector2.txt". | |
In fisierul "vector1.txt", informatia este structurata astfel: | |
- pe prima linie numerele despartite de cate un spatiu; | |
- nu se cunoaste cate numere sunt in fisier. | |
In fisierul "vector2.txt", informatia este structurata astfel: | |
- pe prima linie numarul de elemente; | |
- pe urmatoarea linie elementele despartite prin cate un spatiu. | |
Rezolvare: Deoarece nu se cunoaste numarul de numere din fisier, se citesc pe rand numerele intr-o instructiune while cat | |
timp mai sunt numere in fisier (se testeaza sfarsitul de fisier). Numerele citite vor fi memorate in vectorul x. La fiecare | |
citire se incrementeaza variabila n, la sfarsit aceasta va retine numarul de numere din fisier. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// citire - afisare matrice in fisier | |
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
int main() | |
{ | |
int a[21][21], n, m, i, j, aux, ok; | |
// deschide fisierul 'matrice1.txt' pentru citire: | |
ifstream f("matrice1.txt"); | |
// sau fstream ("matrice1.txt", ios::in); | |
// se testeaza daca fisierul 'matrice1.txt' poate fi deschis: | |
if (!f) | |
{ | |
// afiseaza un mesaj: | |
cout << "Eroare la deschiderea fisierului.\n"; | |
// se paraseste programul: | |
return 0; | |
} | |
// citeste dimensiunile matricei: | |
f >> n >> m; | |
// citeste elementele matricei: | |
for (i = 1; i <= n; i++) | |
for (j = 1; j <= m; j++) | |
f >> a[i][j]; | |
// inchide fisierul 'matrice1.txt': | |
f.close(); | |
// ordoneaza crescator elementele matricei: | |
for (i = 1; i <= n; i++) | |
do | |
{ | |
// initializarea variabilei ok cu 1: | |
ok = 1; | |
for (j = 1; j <= m - 1; j++) | |
// testeaza ordonarea elementelor: | |
if (a[i][j] > a[i][j + 1]) | |
{ | |
// schimba elementele intre ele: | |
aux = a[i][j]; | |
a[i][j] = a[i][j + 1]; | |
a[i][j + 1] = aux; | |
ok = 0; | |
} | |
} while (!ok); | |
// deschide fisierul 'matrice2.txt' pentru scriere: | |
ofstream g("matrice2.txt"); | |
// sau fstream ("matrice1.txt", ios::out); | |
// scrie matricea in fisierul 'matrice2.txt': | |
g << "Matricea ordonata este:\n"; | |
for (i = 1; i <= n; i++) | |
{ | |
for (j = 1; j <= m; j++) | |
g << a[i][j] << " "; | |
// trece pe linia urmatoare: | |
g << endl; | |
} | |
// inchide fisierul 'matrice2.txt': | |
g.close(); | |
// se termina programul: | |
cout << "Gata! Vizualizati fisierul 'matrice2.txt'!\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Citeste din fisierul 'matrice1.txt' o matrice cu n linii si m coloane si scrie matricea in fisierul 'matrice2.txt' dupa | |
ordonarea crescatoare a elementelor fiecarei linii a matricei. | |
In fisierul "matrice1.txt", informatia este structurata astfel: | |
- pe prima linie 2 valori naturale ce reprezinta numarul de linii si numarul de coloane ale matricei; | |
- pe urmatoarele n linii elementele matricei despartite printr-un spatiu. | |
In fisierul "matrice2.txt", informatia este structurata astfel: | |
- pe prima linie mesajul "Matricea ordonata este"; | |
- pe urmatoarea n linii elementele matricei separate printr-un spatiu. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fisier cu numere pare | |
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
int main() | |
{ | |
unsigned a; | |
// deschide fisierul 'numere.txt' pentru citire: | |
ifstream f("numere.txt"); | |
// sau fstream ("numere.txt", ios::in); | |
// se testeaza daca fisierul 'numere.txt' poate fi deschis: | |
if (!f) | |
{ | |
// afiseaza un mesaj: | |
cout << "Eroare la deschiderea fisierului.\n"; | |
// se paraseste programul: | |
return 0; | |
} | |
// deschide fisierul 'pare.txt' pentru scriere: | |
ofstream g("pare.txt"); | |
// sau fstream ("pare.txt", ios::out); | |
f >> a; | |
while (!f.eof()) | |
{ | |
if (a % 2 == 0) | |
g << a << " " << endl; | |
f >> a; | |
} | |
// inchide fisierele: | |
f.close(); | |
g.close(); | |
// se termina programul: | |
cout << "Gata! Vizualizati fisierul 'pare.txt'!\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Fisierul "numere.txt" contine mai multe numere naturale din intervalul [0, 5000] scrise cu spatii intre ele. Se creaza | |
fisierul "pare.txt" care contine, cate una pe linie, doar acele valori din fisierul "numere.txt" care sunt numere pare. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fisier cu numere impare | |
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
int main() | |
{ | |
unsigned a; | |
// deschide fisierul 'numere.txt' pentru citire: | |
ifstream f("numere.txt"); | |
// sau fstream ("numere.txt", ios::in); | |
// se testeaza daca fisierul 'numere.txt' poate fi deschis: | |
if (!f) | |
{ | |
// afiseaza un mesaj: | |
cout << "Eroare la deschiderea fisierului.\n"; | |
// se paraseste programul: | |
return 0; | |
} | |
// deschide fisierul 'impare.txt' pentru scriere: | |
ofstream g("impare.txt"); | |
// sau fstream ("impare.txt", ios::out); | |
f >> a; | |
while (!f.eof()) | |
{ | |
if (a % 2 != 0) | |
g << a << " " << endl; | |
f >> a; | |
} | |
// inchide fisierele: | |
f.close(); | |
g.close(); | |
// se termina programul: | |
cout << "Gata! Vizualizati fisierul 'impare.txt'!\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Fisierul "numere.txt" contine mai multe numere naturale din intervalul [0, 5000] scrise cu spatii intre ele. Se creaza | |
fisierul "impare.txt" care contine, cate una pe linie, doar acele valori din fisierul "numere.txt" care sunt numere impare. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fisier cu numere pozitive | |
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
int main() | |
{ | |
int a; | |
long i; | |
// deschide fisierul 'bac1.txt' pentru citire: | |
ifstream f1("bac1.txt"); | |
// sau fstream ("bac1.txt", ios::in); | |
// se testeaza daca fisierul 'bac1.txt' poate fi deschis: | |
if (!f1) | |
{ | |
// afiseaza un mesaj: | |
cout << "Eroare la deschiderea fisierului.\n"; | |
// se paraseste programul: | |
return 0; | |
} | |
// deschide fisierul 'bac2.txt' pentru scriere: | |
ofstream f2("bac2.txt"); | |
// sau fstream ("bac2.txt", ios::out); | |
for (i = 1; i <= 7000; i++) | |
{ | |
f1 >> a; | |
if (a > 0) | |
f2 << a << " "; | |
} | |
// inchide fisierele: | |
f1.close(); | |
f2.close(); | |
// se termina programul: | |
cout << "Gata! Vizualizati fisierul 'bac2.txt'!\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Fisierul text "bac1.txt" contine 70.000 de numere intregi de cel mult 3 cifre fiecare scrise cu spatii intre ele. Se creaza | |
fisierul "bac2.txt" care contine numai valorile strict pozitive din fisierul "bac1.txt", exact in aceeasi ordine in care se | |
aflau acestea in fisier. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fisier cu numere negative | |
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
int main() | |
{ | |
int a; | |
long i; | |
// deschide fisierul 'bac1.txt' pentru citire: | |
ifstream f1("bac1.txt"); | |
// sau fstream ("bac1.txt", ios::in); | |
// se testeaza daca fisierul 'bac1.txt' poate fi deschis: | |
if (!f1) | |
{ | |
// afiseaza un mesaj: | |
cout << "Eroare la deschiderea fisierului.\n"; | |
// se paraseste programul: | |
return 0; | |
} | |
// deschide fisierul 'bac2.txt' pentru scriere: | |
ofstream f2("bac2.txt"); | |
// sau fstream ("bac2.txt", ios::out); | |
for (i = 1; i <= 7000; i++) | |
{ | |
f1 >> a; | |
if (a < 0) | |
f2 << a << " "; | |
} | |
// inchide fisierele: | |
f1.close(); | |
f2.close(); | |
// se termina programul: | |
cout << "Gata! Vizualizati fisierul 'bac2.txt'!\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Fisierul text "bac1.txt" contine 70.000 de numere intregi de cel mult 3 cifre fiecare scrise cu spatii intre ele. Se creaza | |
fisierul "bac2.txt" care contine numai valorile strict negative din fisierul "bac1.txt", exact in aceeasi ordine in care se | |
aflau acestea in fisier. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// verifica numerele dintr-un interval | |
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
int main() | |
{ | |
long a, b; | |
float x; | |
int ok; | |
// deschide fisierul bac.txt pentru citire: | |
ifstream f("bac.txt"); | |
// sau fstream ("bac.txt", ios::in); | |
// se testeaza daca fisierul poate fi deschis: | |
if (!f) | |
{ | |
// afiseaza un mesaj: | |
cout << "Eroare la deschiderea fisierului.\n"; | |
// se paraseste programul: | |
return 0; | |
} | |
// citeste de la tastatura cele 2 numere: | |
cout << "Alegeti intervalul [a,b]: \n"; | |
cout << "a = "; cin >> a; | |
cout << "b = "; cin >> b; | |
ok = 1; | |
f >> x; | |
// citeste numerele din fisier: | |
while (!f.eof() && ok) | |
{ | |
if (x >= a && x <= b) | |
ok = 0; | |
f >> x; | |
} | |
// inchide fisierul: | |
f.close(); | |
// afiseaza rezultatul: | |
if (ok) | |
cout << "DA"; | |
else | |
cout << "NU"; | |
// trece pe linia urmatoare: | |
cout << endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Citeste din fisierul text "bac.txt" 100000 de numere reale scrise pe prima linie a fisierului, cu spatii intre ele, si de la | |
tastatura 2 numere intregi a si b. Va afisa pe ecran mesajul DA, daca toate numerele din fisier se afla in afara intervalului | |
inchis [a,b] sau mesajul NU, daca cel putin un numar din fisier se afla in intervalul inchis [a,b]. | |
Rezolvare: Presupunem ca toate numerele din fisier se gasesc in afara intervalului [a, b] (variabila ok va fi initializata cu | |
1), se citesc pe rand valorile din fisier si se verifica. Daca una dintre ele este in intervalul [a, b] atunci variabila ok ia | |
valoarea 0. Se va afisa mesajul corespunzator valorii variabilei ok. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fisier cu litere | |
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
int main() | |
{ | |
char ch; | |
unsigned n, i, j; | |
// deschide fisierul pentru scriere: | |
ofstream f("litere.txt"); | |
// sau fstream f("litere.txt, ios::out"); | |
// citeste nr. de linii: | |
cout << "Introduceti numarul de linii:\n"; | |
cout << "n = "; cin >> n; | |
ch = 'A'; | |
// se scriu n linii: | |
for (i = 1; i <= n; i++) | |
{ | |
// pe fiecare linie se scriu ch-64 de caractere ('A' are codul 65): | |
for (j = 1; j <= ch - 64; j++) | |
f << ch; | |
f << endl; | |
ch++; | |
} | |
// inchide fisierul: | |
f.close(); | |
// se termina programul: | |
cout << "Gata! Vizualizati fisierul 'litere.txt'!\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Creeaza un fisier text "litere.txt" cu n linii astfel: | |
- pe prima linie litera A; | |
- pe a doua linie 2 de B; | |
- pe a treia linie 3 de C; s.a.m.d. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// concatenare fisiere | |
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
int main() | |
{ | |
char ch; | |
// deschide fisierul 'fis1.txt' pentru adaugare si 'fis2.txt' pentru citire: | |
fstream f("fis1.txt", ios::app), g("fis2.txt", ios::in); | |
// se testeaza daca fisierul 'fis2.txt' poate fi deschis: | |
if (!g) | |
{ | |
// afiseaza un mesaj: | |
cout << "Eroare la deschiderea fisierului.\n"; | |
// se paraseste programul: | |
return 0; | |
} | |
while (g.get(ch)) | |
f << ch; | |
// inchide fisierele: | |
f.close(); | |
g.close(); | |
// se termina programul: | |
cout << "Gata! Vizualizati fisierul 'fis1.txt'!\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Realizeaza concatenarea a 2 fisiere de tip text si rezultatul se va depune in primul fisier. | |
Rezolvare: Pentru cancatenare se citeste succesiv, din al doilea fisier cate un caracter si se adauga la sfarsitul primului | |
fisier. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fisiere identice | |
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
int main() | |
{ | |
char ch1, ch2; | |
int identic; | |
// deschide fisierele pentru citire: | |
ifstream f("fis1.txt"), g("fis2.txt"); | |
// sau fstream f("fis1.txt", ios::in), g("fis2.txt", ios::in); | |
// se testeaza daca fisierele poate fi deschise: | |
if (!f || !g) | |
{ | |
// afiseaza un mesaj: | |
cout << "Eroare la deschiderea fisierului.\n"; | |
// se paraseste programul: | |
return 0; | |
} | |
identic = 1; | |
while (!f.eof() && !g.eof() && identic) | |
{ | |
f >> ch1; | |
g >> ch2; | |
if (ch1 != ch2) | |
identic = 0; | |
} | |
if (identic) | |
if (!f.eof() || !g.eof()) | |
identic = 0; | |
// afiseaza rezultatul: | |
if (identic) | |
cout << "Fisierele sunt identice.\n"; | |
else | |
cout << "Fisierele nu sunt identice.\n"; | |
// inchide fisierele: | |
f.close(); | |
g.close(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Verifica daca 2 fisiere text sunt diferite din punct de vedere al continutului. | |
Rezolvare: Pentru a verifica daca cele 2 fisiere sunt identice procedam astfel: | |
- presupunem ca sunt identice (variabila identic primeste valoarea 1); | |
- se citeste succesiv cate un caracter din fiecare fisier, intr-o instructiune while, si se compara; | |
- daca caracterele sunt diferite atunci variabila identic ia valoarea 0; | |
- la sfarsit se testeaza si cazul cand unul din fisiere are mai multe caractere, cu toate ca pe lungimea celui mai mic, ele | |
sunt identice. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// stergere - redenumire fisier | |
#include <iostream> | |
#include <fstream> | |
#include <cstdio> // stdio.h pemtru C | |
using namespace std; | |
int main() | |
{ | |
char ch; | |
// deschide fisierul 'date.txt' pentru citire si fisierul 'temp.txt' pentru scriere: | |
fstream f("date.txt", ios::in), g("temp.txt", ios::out); | |
// se testeaza daca fisierul 'date.txt' poate fi deschis: | |
if (!f) | |
{ | |
// afiseaza un mesaj: | |
cout << "Eroare la deschiderea fisierului.\n"; | |
// se paraseste programul: | |
return 0; | |
} | |
while (f.get(ch)) | |
// sau while (ch = f.get()) | |
if (ch != 'p') | |
g << ch; | |
// inchide fisierele: | |
f.close(); | |
g.close(); | |
// sterge fisierul 'date.txt': | |
remove("date.txt"); | |
// redenumeste fisierul 'temp.txt' cu numele fisierului 'date.txt': | |
rename("temp.txt", "date.txt"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sterge din fisierul "date.txt" litera 'p'. | |
Rezolvare: Pentru a sterge litera se copiaza continutul fisierului in alt fisier, mai putin litera 'p', dupa aceea se sterge | |
fisierul initial, iar fisierul obtinut se redenumeste cu numele fisierului initial. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment