samedi 28 mars 2015

QFile opened in QIODevice::Append mode working unexpectedly`; is it a Qt bug?


In my class StudentTable I create a function appendRecord as follow-



qint64 StudentTable::appendRecord(Student student){
QFile file(tableName);
QDataStream stream(&file);
qDebug()<<file.open(QIODevice::Append);
qint64 dumpPosition=file.pos();
qDebug()<<file.openMode();

qDebug()<<dumpPosition;
student.next=-1;
student.print();
stream<<student;
file.close();
return dumpPosition;}


The problem is that when I call the function from main() function directly it works fine. But when I call the same function from the other function in the same class it does nothing; means does not append anything to the file;


Also tried QIODevice::Append, QIODevice::Append|QIODevice::ReadWrite but nothing works for me. I tried because this question : Open QFile for appending says to try something another way; comment of the accepted answer.


I am trying to make a file for students which keeps record of the students. making a record based flat file database; using linked list.


Code: AddRecord() is calling the function appendRecord() in this case nothing happens. But when calling from main() function it appends records in the end.


StudentTable.cpp



#include "studenttable.h"
#include<QtGui>

#include "header.h"

StudentTable::StudentTable()
{
tableName="Student";
}

void StudentTable::create()
{
QFile file(tableName);
if(file.exists())
return;
else
file.open(QIODevice::Append);
QDataStream stream(&file);

Header header;
header.magicNumber=2015;
header.streamVersion=stream.version();
header.used=-1;
header.free=-1;
header.usedLast=-1;
header.freeLast=-1;

stream<<header;
file.close();

}

void StudentTable::setHeader(Header header)
{
QFile file(tableName);
if(!file.exists())
{
qDebug()<<"File "<<tableName<<" doesn't exists";
return;
}
file.open(QIODevice::WriteOnly);
QDataStream stream(&file);

stream<<header;

file.close();
}

Header StudentTable::header()
{
QFile file(tableName);


file.open(QIODevice::ReadOnly);
QDataStream stream(&file);
Header header;
stream>>header;

file.close();
return header;
}

bool StudentTable::isEmpty()
{
Header head=header();
if(head.used==-1)
return true;
else return false;
}

bool StudentTable::isHeapAvailable()
{
Header head=header();
if(head.free==-1)
return false;
else return true;
}

qint64 StudentTable::appendRecord(Student student)
{
QFile file(tableName);
QDataStream stream(&file);
qDebug()<<file.open(QIODevice::Append);
qint64 dumpPosition=file.pos();
qDebug()<<file.openMode();

qDebug()<<dumpPosition;
student.next=-1;
student.print();
stream<<student;
file.close();
return dumpPosition;
}

void StudentTable::print()
{
if(isEmpty())
qDebug()<<" Table is empty";
else
{
Header head=header();
QFile file(tableName);
file.open(QIODevice::ReadOnly);
QDataStream stream(&file);
file.seek(head.used);

Student current;
stream>>current;
current.print();
while(current.next!=-1)
{
file.seek(current.next);
stream>>current;
current.print();

}
file.close();
}

}
bool StudentTable::addRecord(Student student)
{
if(isEmpty()&&!isHeapAvailable())
{
qDebug()<<"case 1";
student=Student("rajesh","ramesh",4,5,6);
qint64 dumpPosition= appendRecord(student);
Header head=header();
head.used=dumpPosition;
head.usedLast=dumpPosition;
setHeader(head);
return true;
}

if(!isEmpty()&&!isHeapAvailable())
{
qDebug()<<"case 2";
qint64 dumpPosition= appendRecord(student);
Header head=header();

head.usedLast=dumpPosition;
setHeader(head);
return true;
}

return false;

}

QStandardItemModel * StudentTable::getModel()
{
QStandardItemModel *model;
return model;

}


main.cpp



#include "widget.h"
#include <QApplication>
#include "Student.h"
#include "studenttable.h"
#include<QDebug>
#include<QtGui>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

StudentTable studentTable;
studentTable.create();
Header header;
header=studentTable.header();
header.debug();

for(int i=1;i<2;i++)
{
Student student = Student("ram","bhairu",i,i,i);
studentTable.addRecord(student);

}

header=studentTable.header();
header.debug();

//studentTable.print();

Widget widget;
// widget.table->setModel(model);
widget.show();

return a.exec();
}



Aucun commentaire:

Enregistrer un commentaire