Read date of a file

0

I want to read a date from a file in the following way:

for example

void Store::leerUsuarios(const string &nombreFichero){
 ifstream fichero;
 fichero.open(nombreFichero);
 if (fichero.is_open()) {
    string nom, mail;
    int dia; int mes; int anyo;


    while ((fichero >> nombre) && (fichero >> mail) && (fichero >> dia) && 
    (fichero >> mes) && (fichero >> anyo)) {
        fichero >> nombre;
        fichero >> mail;
        fichero >> dia;
        fichero >> mes;
        fichero >> anyo;
         }
    fichero.close();
     }
 }

with fstream in a file it is separated by spaces, but if I have a date for example:

01/01/2000

How do I make it so that I can read that?

    
asked by Chariot 30.09.2018 в 23:11
source

1 answer

3

If we assume that the date will be well written, the reading can be quite simple:

ifstream fichero;
// abre el fichero..

int dia, int mes, int anio;
fichero >> dia;
fichero.ignore(); // Descartamos la primera barra
fichero >> mes;
fichero.ignore(); // Descartamos la segunda barra
fichero >> anio;

We can save the ignore if we use a variable type char :

int dia, int mes, int anio;
char separador;

fichero >> dia >> separador >> mes >> separador >> anio;
    
answered by 01.10.2018 / 09:18
source