Created
February 12, 2025 16:43
-
-
Save ProfAndreaPollini/45af49b6312455bd7b537a59610e214e to your computer and use it in GitHub Desktop.
video yt sui file
This file contains hidden or 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
#include <iostream> | |
#include <fstream> | |
#include <vector> | |
#include <sstream> | |
struct Persona { | |
std::string nome; | |
std::string cognome; | |
int matricola; | |
}; | |
void to_csv(std::string,std::vector<Persona>); | |
std::vector<Persona> from_csv(std::string filename); | |
int main() { | |
std::vector<Persona> persone = { | |
{"Pinco", "Pallo", 1}, | |
{"Mario", "Strani", 2} | |
}; | |
to_csv("persone.csv",persone); | |
auto result = from_csv("persone.csv"); | |
to_csv("result.csv",result); | |
// std::ofstream outfile("out.txt"); | |
// | |
// if (outfile.is_open()) { | |
// outfile << "Hello World2!\n"; | |
// outfile.close(); | |
// } | |
// | |
// std::ifstream infile("out.txt"); | |
// std::string line; | |
// if (infile.is_open()) { | |
// while (std::getline(infile, line)) { | |
// std::cout << line << "\n"; | |
// } | |
// infile.close(); | |
// } | |
return 0; | |
} | |
std::vector<Persona> from_csv(std::string filename) { | |
std::ifstream file(filename); | |
std::vector<Persona> persone; | |
std::string line; | |
if (file.is_open()) { | |
while (std::getline(file, line)) { | |
std::stringstream ss(line); | |
std::string token; | |
Persona persona; | |
std::getline(ss, token, ';'); | |
persona.nome = token; | |
std::getline(ss, token, ';'); | |
persona.cognome = token; | |
std::getline(ss, token); | |
persona.matricola = std::stoi(token); | |
persone.push_back(persona); | |
} | |
file.close(); | |
} | |
return persone; | |
} | |
void to_csv(std::string filename,std::vector<Persona> data) { | |
std::ofstream output{filename}; | |
if (output.is_open()) { | |
for (const auto& persona : data) { | |
output << persona.nome << ";"; | |
output << persona.cognome << ";"; | |
output << persona.matricola << std::endl; | |
} | |
output.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment