
Encontrar datos dentro de un QDataStream tipo class
Publicado por Manu (5 intervenciones) el 12/09/2017 08:32:57
Hola amigos, estoy probando a buscar dentro de un archivo que e guardado como class nombres edades y demas, e empezado intentanto QString == Persona, pero evidentemente es imposible. Pienso que quizas e de convertir los datos recibidos a QString y sobre esos datos hacer la busqueda del nombre o de la edad, pero no se como. Muchas gracias de antemano por la ayuda.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef PERSONA_H
#define PERSONA_H
#include <QDebug>
#include <QObject>
class Persona : public QObject
{
Q_OBJECT
public:
explicit Persona(QObject *parent = nullptr);
void setNombre(const QString &);
void setEdad(int);
void getNombre() const;
void getEdad() const;
friend inline QDataStream &operator <<(QDataStream &d, const Persona &p);
friend inline QDataStream &operator >>(QDataStream &d, Persona &p);
friend inline QDebug operator <<(QDebug d, const Persona &p);
signals:
public slots:
private:
QString nombre;
int edad;
};
#endif // PERSONA_H
#include "persona.h"
#include <QDebug>
#include <QFile>
#include <QTextStream>
Persona::Persona(QObject *parent) : QObject(parent)
{
}
void Persona::setNombre(const QString &nombre1)
{
nombre = nombre1;
}
void Persona::setEdad(int edad1)
{
edad = edad1;
}
void Persona::getNombre() const
{
qDebug()<< nombre;
}
void Persona::getEdad() const
{
qDebug()<< edad;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <QCoreApplication>
#include "persona.h"
#include <QFile>
#include <QDataStream>
#include <QDebug>
inline QDataStream &operator<<(QDataStream &d, const Persona &p)
{
return d << p.nombre << p.edad;
}
inline QDataStream &operator>>(QDataStream &d, Persona &p)
{
return d >> p.nombre >> p.edad;
}
inline QDebug operator <<(QDebug d, const Persona &p)
{
return d << p.nombre << p.edad;
}
bool muestra();
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Persona persona;
persona.setNombre("Pedro corso");
persona.setEdad(89);
QFile archivo("usuario.txt");
QDataStream io;
archivo.open(QIODevice::WriteOnly | QIODevice::Append);
if(!archivo.isOpen()){
qDebug()<< "Error en la apertura del archivo 1";
return 1;
}
io.setDevice(&archivo);
io << persona;
archivo.flush();
archivo.close();
bool y;
y = muestra();
qDebug()<< "";
qDebug()<< "";
qDebug()<< y;
qDebug()<< " Fin del programa ";
return a.exec();
}
bool muestra()
{
qDebug()<< " **** Muestra **** ";
Persona buscar = "Pedro";
bool band=false;
Persona x;
QFile archivo("usuario.txt");
QDataStream io;
archivo.open(QIODevice::ReadOnly);
if(!archivo.isOpen()){
qDebug()<< " Error en la apertura del archivo ";
}
io.setDevice(&archivo);
while(!archivo.atEnd()){
io >> x;
qDebug() << x;
if(x==buscar){
band = true;
}
}
archivo.flush();
archivo.close();
return band;
}
Valora esta pregunta


0