Jump to content
Luffy26

lucru cu fisiere in c/c++

Recommended Posts

Pai in primul rand, cum sunt salvate datele in fisierul de intrare?

in principiu pentru c++ poti lua ca exemplu aici

Iar pentru c aici

Ideea e urmatoarea in ambele cazuri

Deschizi fisierul de intrare si de iesire, te asiguri ca ambele fisiere sunt deschise, dupa care citesti niste date din fisierul de intrare, le salvezi intr-o variabila, verifici conditia, si daca e indeplinita scrii variabila in fisierul de iesire.

Repeti pana cand nu mai ai date in fisierul de intrare

Inchizi fisierul de intrare si fisierul de iesire pentru a se salva modificarile.Cam asta e tot.

Citeste in tutorialele pe care ti le-am data, scrie tu niste cod, si daca ai probleme posteaza aici si te ajut. Dar numai dupa ce ai depus si tu efortul de a incerca macar sa rezolvi singur problema.

Link to comment
Share on other sites

C:


#include <stdio.h>
#include <stdlib.h>

int main() {
FILE *fin, *fout;
char nume[20], prenume[20];
int nr_matricol;
float medie;

if (!(fin = fopen("in.txt", "r")) || !(fout = fopen("out.txt", "w"))) {
printf("Eroare deschidere fisier de intrare!\n");
exit(EXIT_FAILURE);
}

while (!feof(fin)) {
fscanf(fin, "%s %s %d %f\n", nume, prenume, &nr_matricol, &medie);
if (medie >= 5)
fprintf(fout, "%s %s %d %f\n", nume, prenume, nr_matricol, medie);
}

fclose(fin);
fclose(fout);
}

C++:


#include <iostream>
#include <fstream>

using namespace std;

class Student {
string nume, prenume;
int nr_matricol;
float medie;
public:
friend istream &operator>>(istream &, Student &);
friend ostream &operator<<(ostream &, Student);
bool Absolvent();
};

istream &operator>>(istream &in, Student &s) {
in >> s.nume >> s.prenume >> s.nr_matricol >> s.medie;
return in;
}

ostream &operator<<(ostream &out, Student s) {
out << s.nume << ' ' << s.prenume << ' ' << s.nr_matricol << ' ' << s.medie << '\n';
return out;
}

bool Student::Absolvent() {
return medie >= 5;
}

int main() {
Student student;

ifstream fin("in.txt");
ofstream fout("out.txt");

while (fin >> student)
if (student.Absolvent())
fout << student;

fin.close();
fout.close();

return 0;
}

Edited by totti93
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...