I can not get a file integer

1

Tema.h

#ifndef TEMA_H
#define TEMA_H
#include <string>
#include <fstream>**
using namespace std;

typedef struct{
    string title;
    string intr;
    int seg;
}tTema;

bool cargar(tTema &tema);
void mostrar(tTema tema);
#endif

Tema.cpp

#include "Tema.h"
#include <iostream>
using namespace std;

int main(){

    tTema tema;
    cargar(tema);
}
bool cargar(tTema &tema){

    int num;
    bool ok = true;

    ifstream archivo;
    archivo.open("tema.txt");

    if (archivo.is_open()){
        archivo >> num;
        archivo >> tema.title;
        archivo >> tema.intr;
        archivo >> tema.seg;
    }
    else {
        ok = false;
    }
    return ok;
}

I know I need to finish the code but I have tried many things and the "int sec" never take them. The other elements of the structure take them well, any ideas?

The txt is like this

5
Volvera
Dani Martin
120
    
asked by euridicegreyback 03.09.2016 в 14:16
source

2 answers

4

The problem you have is associated with handling the streams of the standard library of c++ .

When you want to take the value for intr instead of taking "Dani Martin" it takes "Dani", then you want to put a value type string in a type int , although it does not give an execution error, no you get the value you want (neither in intr , nor in seg ).

You could solve the issue using getline , but you have blank lines that you want to ignore and getline as opposed to >> does not ignore them.

An example of a solution is the following, it is very improvable, but it is a beginning.

tema.h

#ifndef TEMA_H
#define TEMA_H
#include <string>
#include <fstream>

typedef struct{
    std::string title;
    std::string intr;
    int seg;
}tTema;

bool cargar(tTema &tema);
void mostrar(tTema tema);
#endif

tema.cpp

#include "tema.h"
#include <iostream>
#include <sstream>

int main(){

    tTema tema;
    cargar(tema);
}
bool cargar(tTema &tema){

    int num;
    bool ok = true;

    std::ifstream archivo;
    archivo.open("tema.txt");

    //Variable auxiliar para saber linea con informacion es 
    int datosLeidos= 0;

    //Variable auxliar para leer las lineas del archivo
    std::string linea; 


    while (std::getline(archivo, linea)) {
      //Si la linea esta vacia, continua con la proxima
      if (linea.empty()) continue;

      std::stringstream is(linea);

      //Dependiendo de la linea leida
      //es la variable a la cual se asocia el valor  
      switch(datosLeidos) { 
        case 0:
          is >> num;
          break;
        case 1:
          is >> tema.title;
          break;
        case 2:
          //Necesitas tomar toda la linea
          getline(is, tema.intr);
          break;
        case 3:
          is >> tema.seg;
          break;
        default:
          std::cout << "Ya se leyeron 4 lineas. No hace nada.";
      }
      datosLeidos++;

    }
    //Si cargo los datos entonces esta ok.

    ok= (4 >= datosLeidos);

    //Para ver que hizo lo esperado 
    std::cout << num << std::endl;
    std::cout << tema.title << std::endl;
    std::cout << tema.intr << std::endl;
    std::cout << tema.seg << std::endl;

    return ok;
}

If you are going to keep doing things in C++ with input / output, I suggest you look at the Boost libraries on the subject ( Boost I / O ).

References (in English):

SO: Reading string with spaces in c ++

stringstream on cplusplus.com

    
answered by 03.09.2016 в 19:32
2

The > > > operator's problem is that it reads until you find a space ( '' ), a tabulator ( '\ t' ) or a line break ( '\ n' ). If you have the phrase "Dani Martin" and try to read it with file > > tema.intr , intr will only contain "Dani" , leaving "Martin" for the next reading, which you also expect to read a int last, for that reason does not turn out well.

To read something like that in the file:

  

5
  Will you go back
  Dani Martin
  120

You need that:

// ...

string firstName, secondName;

ifstream archivo;
archivo.open("tema.txt");

if ( archivo.is_open() ){
    archivo >> num; // Leer el primer int
    archivo >> tema.title; //Leer a "Volvera"
    archivo >> firstName >> secondName; //firstName="Dani" y secondName="Martin"

    tema.intr = firstName + " " + secondName ; // Unimos el primer nombre con el segundo y lo asignamos a tema.intr

    archivo >> tema.seg; //Leemos el ultimo int

    archivo.close(); // Importante cerrar antes de salir

    //imprimimos los resultados para ver si todo va ok.
    cout << tema.title << endl;
    cout << tema.intr << endl;
    cout << tema.seg << endl;
    //resto del programa...

I think that's what you're looking for. Greetings.

    
answered by 03.09.2016 в 19:34