0

我在 QDataStream 中存储了一些数据并立即获取数据,但是在检索时计数显示为零。代码看起来不错,但行为出乎意料

//Overloading
QDataStream& operator<< (QDataStream& writeTO, const CascadeJobInfo& data)
{
    writeTO << data.m_infoJobType << data.m_connectionName << data.m_submitJobId << data.m_submitJobStat;
    return writeTO;
}

QDataStream& operator>> (QDataStream& readIn, CascadeJobInfo& data)
{
    readIn >> data.m_infoJobType >> data.m_connectionName >> data.m_submitJobId >> data.m_submitJobStat;
    return readIn;
}

void Fun()
{
        // Code Starts here
    projectFileName = /*Path to folder*/
    QFile file(projectFileName);
    file.open(QFile::ReadWrite);
    file.close();

    QDataStream dStream(&file);
    int jobLstCount = /*Get the Count, assume 4*/
    dStream << jobLstCount;

    for(int i = 0; i < jobLstCount; i++)
    {
    JobInfo.m_infoJobType = jobFlowItem->getJobType();
    JobInfo.m_connectionName = submitItem->connectionName();
    JobInfo.m_submitJobId = submitItem->jobID();
    JobInfo.m_submitJobStat = submitItem->jobState();
        // All valid data stored here
    }

    file.close();

    QDataStream dStreamOut(&file);
    dStreamOut >> jobLstCount; /*Count returns zero here insted of 4*/
    CascadeJobInfo jobInfo;
    // Why jobLstCount is getting zero here
    for(int i = 0 ; i < jobLstCount ; i++)
    {
    dStreamOut >> jobInfo;
    } 
}
4

1 回答 1

0
   file.open(QFile::ReadWrite);
   file.close(); <--- HERE

   QDataStream dStream(&file);

你一打开文件就关闭它,所以基本上你正在使用一个无效的文件描述符,它不起作用。完成后将 file.close() 放在代码的末尾。

于 2017-11-23T12:41:22.470 回答