#ifndef LOADCALENDAR_HH #define LOADCALENDAR_HH #include <vector> #include <string> bool loadCalendar(const std::string& filename, std::vector<std::string>& calendar); #endif // LOADCALENDAR_HH
#include "LoadCalendar.hh" #include <sstream> #include <fstream> bool loadCalendar(const std::string& filename, std::vector<std::string>& calendar) { std::ifstream file(filename.c_str()); if (!file) return false; std::string line; while (std::getline(file, line)) { std::istringstream row(line); std::string word; while (std::getline(row, word, ';')) calendar.push_back(word); } return true; }
#include "LoadCalendar.hh" int main() { const std::string filename = "Calen5Jours.txt"; std::vector<std::string> calendar; if (!loadCalendar(filename, calendar)) { std::cout << "Can't open " << filename << std::endl; return 1; } for (int i = 0; i < calendar.size(); ++i) { for (int j = 0; j < 7; ++j) std::cout << calendar[i * 7 + j] << " "; std::cout << std::endl; } return 0; }
Améliorer votre expérience CodeS-SourceS avec ce plugin:
http://codes-sources.commentcamarche.net/forum/affich-10000111-plugin-better-cs-2#cptpingu-signature
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPar contre le programme plante après ouverture de la fenêtre
1;2;3
4
Pourquoi le nom de fichier est passé en constante?
Pourquoi utiliser des pointeurs pour filename et calendar?
Dans main, pourquoi redéclarer std::vector<std::string> calendar; est-ce que cela ne réinitialise pas la variable? Elle est déjà déclarée dans LoadCalendar.hh et celui-ci est inclus dans main, pourquoi main ne connait pas cette variable?
Améliorer votre expérience CodeS-SourceS avec ce plugin:
http://codes-sources.commentcamarche.net/forum/affich-10000111-plugin-better-cs-2#cptpingu-signature