69,336
社区成员
发帖
与我相关
我的任务
分享
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
typedef struct
{
int flag;
bool punched;
}item_info;
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void init_func();
private:
Ui::Widget *ui;
item_info **p1_list;
item_info **p_list;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <stdio.h>
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
init_func();
}
Widget::~Widget()
{
// delete p_list;
// delete p1_list;
delete ui;
}
void Widget::init_func()
{
int rows = 8;
int columns = 12;
p1_list = new item_info*[rows];
p_list = new item_info*[rows];
for (int i = 0; i < rows; i++)
{
p1_list[i] = new item_info[columns];
p_list[i] = new item_info[columns];
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
p1_list[i][j].flag = j;
p_list[i][j].flag = 0;
p1_list[i][j].punched = false;
p_list[i][j].punched = false;
}
}
FILE *fp = fopen("./table1", "rb+");
if (fp)
{
qDebug() << fwrite(&p1_list, sizeof(item_info), 96, fp);
fseek(fp, 0, SEEK_SET);
qDebug() << fread(&p_list, sizeof(item_info), 96, fp);
}
fclose(fp);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
qDebug() << "(" << i << j << ")" << p_list[i][j].flag;
}
}
return;
}
QFile file("./table1");
bool ret = file.open(QIODevice::ReadWrite|QIODevice::Truncate);
QDataStream out(&file);
if (!ret)
{
qDebug() << "open table1 failed";
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
out << p1_list[i][j].flag << p1_list[i][j].punched;
}
}
file.flush();
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
out >> p_list[i][j].flag >> p_list[i][j].punched;
}
}
file.close();
这么写岂不是很麻烦么?