Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.
|
|
Source code |
1 2 3 4 |
typedef struct user {
Qstring user;
QString pass;
} user;
|
|
|
Source code |
1 2 3 |
QFile mapfile(filename); mapfile.open(QIODevice::ReadWrite); //how to proceed from here? |
|
|
Source code |
1 2 3 4 5 6 7 |
QFile mapfile(filename);
if (!mapfile.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QTextStream out(&mapfile);
out << x.user << "\n";
out << x.pass << "\n";
|
|
|
Source code |
1 2 3 4 5 6 7 |
QFile mapfile(filename); if (!mapfile.open(QIODevice::ReadOnly | QIODevice::Text)) return; QTextStream in(&mapfile); x.user = in.readLine(); x.pass = in.readLine(); |
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// .h file
class User
{
QString name;
QString pass;
friend QDataStream & operator << ( QDataStream & s, const User & u );
friend QDataStream & operator >> ( QDataStream & s, User & u );
}:
// friends operators need to be declared outside of the class
QDataStream & operator << ( QDataStream & s, const User & u );
QDataStream & operator >> ( QDataStream & s, User & u );
// .cpp file
QDataStream & operator << ( QDataStream & s, const User & u )
{
return( s << u.name << u.pass );
}
QDataStream & operator >> ( QDataStream & s, User & u )
{
return( s >> u.name >> u.pass );
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// .h file
class User
{
QString name;
QString pass;
QDataStream& printIt( QDataStream & s ) const;
QDataStream& readIt( QDataStream & s );
}:
// friends operators need to be declared outside of the class
QDataStream & operator << ( QDataStream & s, const User & u ) { return u.printIt(s); }
QDataStream & operator >> ( QDataStream & s, User & u ) { return u.readIt(s); }
// .cpp file
QDataStream& User::printIt( QDataStream & s ) const
{
return( s << name << pass );
}
QDataStream& User::readIt( QDataStream & s )
{
return( s >> name >> pass );
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
struct place {
QString placename;
QString x;
QString y;
};
struct group {
QString grpname;
QList<place> building;
};
struct mapinfo {
QString map_path;
QList<group> grp;
};
|
|
|
Source code |
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
#include <QtCore/QCoreApplication>
#include <QList>
#include <QString>
#include <QFile>
#include <QTextStream>
struct place {
QString placename;
QString x;
QString y;
place()
{
}
place(const QString& p1, const QString& p2, const QString& p3) : placename(p1), x(p2), y(p3)
{
}
QTextStream& print(QTextStream& os) const
{
os << placename << "\n" << x << "\n" << y << "\n";
return os;
}
QTextStream& read(QTextStream& os)
{
placename = os.readLine();
x = os.readLine();
y = os.readLine();
return os;
}
};
struct group {
QString grpname;
QList<place> building;
QTextStream& print(QTextStream& os) const;
QTextStream& read(QTextStream& os);
};
struct mapinfo {
QString map_path;
QList<group> grp;
};
QTextStream& operator<<(QTextStream& os, place p)
{
return p.print(os);
}
QTextStream& operator<<(QTextStream& os, group g)
{
return g.print(os);
}
QTextStream& group::print(QTextStream& os) const
{
os << grpname << "\n";
os << building.count() << "\n";
foreach (place p, building)
{
os << p;
}
return os;
}
QTextStream& operator>>(QTextStream& os, place& p)
{
return p.read(os);
}
QTextStream& operator>>(QTextStream& os, group& g)
{
return g.read(os);
}
QTextStream& group::read(QTextStream& os)
{
bool ok;
building.clear();
grpname = os.readLine();
int cnt = os.readLine().toInt(&ok, 10);
for (int i=0; i<cnt; ++i)
{
place p;
os >> p;
building << p;
}
return os;
}
int main(int argc, char *argv[])
{
group x;
x.grpname = "GROUP Name";
// Add two places into the list.
x.building << place("one", "two", "three") << place("four", "five", "six");
QString fileName("tmp.txt");
QFile file(fileName);
// At this point, decide if you want to use a binary file, or a text file.
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream os1(&file);
os1 << x;
file.close();
group y;
file.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream is1(&file);
is1 >> y;
file.close();
qDebug(qPrintable(QString("group name:%1 with %2 places").arg(y.grpname).arg(y.building.count())));
foreach (place p, y.building)
{
qDebug(qPrintable(QString(" +(%1)(%2)(%3)").arg(p.placename).arg(p.x).arg(p.y)));
}
return 0;
}
|
This post has been edited 1 times, last edit by "eva2002" (Jan 26th 2010, 2:54am)
|
|
Source code |
1 2 3 4 5 6 |
#ifndef PLACES_H #define PLACES_H this is where your primary definitions go #endif |
|
|
Source code |
1 2 3 4 |
QTextStream& operator<<(QTextStream& os, place p)
{
return p.print(os);
}
|
|
|
Source code |
1 2 3 4 |
inline QTextStream& operator<<(QTextStream& os, place p)
{
return p.print(os);
}
|