Actualiser db.cpp

This commit is contained in:
2026-02-20 14:30:21 +00:00
parent a340a830bc
commit de7d24858f

39
db.cpp
View File

@@ -2,31 +2,48 @@
#include <fstream>
#include <iostream>
bool exportData(const std::string& filename, const std::vector<Student>& students) {
bool exportData(const std::string& filename, const std::vector<Student>& students, const std::string& role, const std::string& login){
std::ofstream file(filename);
if(!file.is_open()) return false;
for(auto& s : students){
file << s.nom << " " << s.notes[0] << " " << s.notes[1] << " " << s.notes[2] << "\n";
if(role == "ADMIN" || role == "PROF"){
for(const auto& s : students){
file << s.nom << " " << s.notes[0] << " " << s.notes[1] << " " << s.notes[2] << "\n";
}
} else if(role == "STUDENT"){
for(const auto& s : students){
if(s.nom == login){
file << s.nom << " " << s.notes[0] << " " << s.notes[1] << " " << s.notes[2] << "\n";
}
}
}
file.close();
return true;
}
bool importData(const std::string& filename, std::vector<Student>& students) {
bool importData(const std::string& filename, std::vector<Student>& students, const std::string& role){
if(role == "STUDENT"){
std::cout << "Import interdit pour STUDENT.\n";
return false;
}
std::ifstream file(filename);
if(!file.is_open()) return false;
std::string nom;
int n1,n2,n3;
int i = 0;
while(file >> nom >> n1 >> n2 >> n3 && i < students.size()){
students[i].nom = nom;
students[i].notes[0] = n1;
students[i].notes[1] = n2;
students[i].notes[2] = n3;
i++;
while(file >> nom >> n1 >> n2 >> n3){
for(auto& s : students){
if(s.nom == nom){
s.notes[0] = n1;
s.notes[1] = n2;
s.notes[2] = n3;
}
}
}
file.close();
return true;
}