Gestionnaire de fichier en C
Guide pratique : Gestionnaire de fichier en C. Recherche parmi 300 000+ dissertationsPar Meatballz • 11 Juillet 2020 • Guide pratique • 823 Mots (4 Pages) • 398 Vues
#include <string>
#include <fstream>
#include <sstream>
#include <math.h>
#include "file_reader.hh"
int cases;
static int extract_integer(std::string& str)
{
std::stringstream ss;
ss << str;
std::string tmp;
int res;
while(!ss.eof())
{
ss >> tmp;
if(std::stringstream(tmp) >> res)
return res;
}
return -1;
}
std::vector<struct price_soup> read_file(std::string& filename)
{
std::vector<price_soup> samples;
std::ifstream infile;
infile.open(filename);
infile >> cases;
std::string skip;
int options;
int flavours;
size_t price;
int k = 0;
while (infile && k < cases)
{
struct price_soup p_s;
infile >> price;
infile >> flavours;
p_s.price = price;
for(int i = 0 ; i < flavours; ++i)
{
int target;
infile >> skip;
infile >> target;
p_s.flavours.push_back(target);
}
infile >> options;
for(int i = 0 ; i < options; ++i)
{
std::string target;
infile >> target;
p_s.options.push_back(extract_integer(target));
}
samples.push_back(p_s);
k++;
}
infile.close();
return samples;
}
static int get_best_opt(int price,
...