#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
qulonglong getFreeSpace(const QString &path);
struct my_struct
{
char name[9];
char fam[9];
int nomer;
int age;
}m_str;
int z;
QList<my_struct> lst;
friend QDataStream &operator<<(QDataStream & ds, const my_struct &calData)
{
ds.writeRawData((char*)calData.fam,8);
ds.writeRawData((char*)calData.name,8);
ds << calData.age << calData.nomer;
return ds;
}
friend QDataStream &operator>>(QDataStream & ds, my_struct &calData)
{
ds.readRawData((char*)calData.fam, 8);
calData.fam[8] = '\0';
ds.readRawData((char*)calData.name, 8);
calData.name[8] = '\0';
ds >> calData.age >> calData.nomer;
return ds;
}
private slots:
void on_pushButton_2_clicked();
void on_pushButton_clicked();
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDir>
#include <QDebug>
#include <QFile>
#include <QDataStream>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QString name = "alexalex";
QByteArray ba = name.toLatin1();
const char *c_str2 = ba.data();
m_str.age = 8;
m_str.nomer = 4;
strcpy(m_str.name, "alexalex");
strcpy(m_str.fam, "famafama");
qDebug() << sizeof(m_str.name);
lst.append(m_str);
QFile file("test.txt");
file.open(QIODevice::WriteOnly|QIODevice::Append);
QDataStream stream(&file);
stream << m_str;
}
void MainWindow::on_pushButton_2_clicked()
{
QFile file("test.txt");
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
in >> m_str;
QString buf(m_str.name);
qDebug() << buf;
qDebug() << m_str.name << m_str.fam << m_str.age << m_str.nomer;
}
|