QT文件打开不会创建文件

时间:2012-08-20 14:32:07

标签: c++ qt qfile

我正在尝试使用QT打开文件并写入文件:

void fileClass::saver(QList<ToDo *> t, QList<appointment *> a, QString fileName)
{
    fileName.append(".mca");
    file = new QFile(fileName);
    if( !file->open(stdin,QIODevice::WriteOnly))
        qDebug() << "Could not create file " << fileName << endl;
    for (int i = 0; i < t.count(); i++)
    {
        file->write(t[i]->ShowDate().toString().toAscii().constData(), qstrlen(t[i]->ShowDate().toString().toAscii().constData()));
        file->write(t[i]->ShowDescription().toAscii().constData(), qstrlen(t[i]->ShowDescription().toAscii().constData()));
        file->write(t[i]->ShowStart().toString().toAscii().constData(), qstrlen(t[i]->ShowStart().toString().toAscii().constData()));
        file->write(t[i]->ShowTitle().toAscii().constData(), qstrlen(t[i]->ShowTitle().toAscii().constData()));
        file->write(t[i]->ShowWarning().toString().toAscii().constData(), qstrlen(t[i]->ShowWarning().toString().toAscii().constData()));
        file->write(t[i]->ShowWarningTime().toString().toAscii().constData(), qstrlen(t[i]->ShowWarningTime().toString().toAscii().constData()));
        switch (t[i]->ShowPriority())
        {
        case (VERY_HIGH):
            file->write("0\0");
            break;
        case (HIGH):
            file->write("1\0");
            break;
        case (NORMAL):
            file->write("2\0");
            break;
        case (LOW):
            file->write("3\0");
            break;
        case (VERY_LOW):
            file->write("4\0");
            break;
        }
        switch (t[i]->ShowState())
        {
        case (STILL_BEGIN):
            file->write("0\0");
            break;
        case (BEGUN):
            file->write("1\0");
            break;
        case (ALMOST_DONE):
            file->write("2\0");
            break;
        case (DONE):
            file->write("3\0");
            break;
        }
    }
    file->write("----------------------------------------------------------");
    for(int i =0; i<a.count(); i++)
    {
        file->write(a[i]->ShowDate().toString().toAscii().constData(), qstrlen(a[i]->ShowDate().toString().toAscii().constData()));
        file->write(a[i]->ShowDescription().toAscii().constData(), qstrlen(a[i]->ShowDescription().toAscii().constData()));
        file->write(a[i]->ShowEnd().toString().toAscii().constData(), qstrlen(a[i]->ShowEnd().toString().toAscii().constData()));
        file->write(a[i]->ShowReminder().toString().toAscii().constData(), qstrlen(a[i]->ShowReminder().toString().toAscii().constData()));
        file->write(a[i]->ShowStart().toString().toAscii().constData(), qstrlen(a[i]->ShowStart().toString().toAscii().constData()));
        file->write(a[i]->ShowTitle().toAscii().constData(), qstrlen(a[i]->ShowTitle().toAscii().constData()));
    }
    file->close();
}

然而文件没有以某种方式创建?任何想法为什么?我自己做了扩展名.mca ......

文件名正确传递没有任何问题...并且调试没有显示,所以理论上它应该工作但我的文件夹中找不到文件... 提前致谢

2 个答案:

答案 0 :(得分:1)

您似乎需要使用其他QFile::open方法。您使用的方法打开现有文件句柄,因此您打开stdin。您之前设置的文件名无关紧要。

试试这个:

if( !file->open(QIODevice::WriteOnly))
        qDebug() << "Could not create file " << fileName << endl;

答案 1 :(得分:1)

这个对stdin的引用看起来很虚伪:

   if( !file->open(stdin,QIODevice::WriteOnly))

stdin用于阅读,而不是用于写作。

更好地使用此表单:

bool QFile::open ( OpenMode mode )

其中modeQIODevice::WriteOnly