[SOLVED]Empty file on creation



  • I have created a small ans simple method, which is supposed to create a simple textfile and write some stuff to it.

    I really does create the folders and the file at the right place, but the textfile is empty every time.

    QFile::error() returns '0' - which in my book translates into "the file is opend correctly".

    If anybody is able to give me a pointer, I would very much appreciate it :o)

    Thanks in advance :o)


    From the class definition:

    @ QString configBasePath;
    QString configFolderPath;
    QString configfilePath;
    int defaultIdleTimeout;@


    From the constructor:
    @
    QString homeLocation = QStandardPaths::locate(QStandardPaths::HomeLocation, QString(), QStandardPaths::LocateDirectory);

    this->configBasePath = homeLocation+".config/";
    this->configFolderPath = homeLocation+".config/QSaver/";
    this->configfilePath = homeLocation+".config/QSaver/QSaver.conf";
    this->defaultIdleTimeout = 5;
    this->myDEBUG = true;@
    

    @/* This one creates a new config file with a standard or user defined timeout

    • Out : 0 Successfully created config file
    •   1 Error creating folder
      
    •   2 Error creating file
      

    */
    int QSaverControlCenter::createNewConfigFile(){

    QDir myDir(this->configFolderPath);
    
    if(!myDir.exists())
    {
        myDir.mkpath(this->configFolderPath);
    
        if(!myDir.exists())
        {
            return 1;
        }
        else
            if (this->myDEBUG)
    

    {
    qDebug() << "QSCC::createNewConfigFile - Config folder successfully created";
    }
    }

    QFile newfile&#40;this->configfilePath&#41;;
    
    if (newfile.open(QIODevice::WriteOnly | QIODevice::Text&#41;)
    {
        QTextStream stream(&newfile);
        stream << "<TIME>" << this->defaultIdleTimeout << "</TIME>" << endl;
        qDebug() << "##" << newfile.error();  //Is this thing on ???
        newfile.close();     
    
        qDebug() << this->defaultIdleTimeout;   //temp for show and tell
    
    }
    else
        return 2;
    
    return 0;
    

    }@


    qDebug method output :

    @5

    0 @


    Many regards
    Peter



  • What if you use QIODevice::ReadWrite for the open?



  • Then it works perfectly - the first time. After that it returns to its old habbits, creating an empty file.

    Between takes I have cleaned and removed the executable, to ensure that it is fresh.



  • Hmm,
    Oke, when the file exists you should remove it first (before the open()). Then a new file is created. Then the stream will be working. If no new file is needed and only an append is needed, it might be wise to set your stream to the end of the file before writing. Because of HDD space etc it's impossible to simply write somewhere in the middle of a file.
    Greetz



  • The createNewConfigFile() only gets invoked when a QSaver.conf file, do not exist. The metod below is run initially. If it returns false, then the createNewConfigFile() is run else another method which reads the QSaver.conf and extracts the items.

    @// Does the config file exist ?
    bool QSaverControlCenter::isConfigFile(){

    QFileInfo myFile&#40;this->configfilePath&#41;;
    

    if(myFile.exists() == false)
    {
    if (this->myDEBUG)
    {
    qDebug() << "QSCC::isConfigFile - Config file do not exist";
    }
    return false;
    }
    else
    {
    if (this->myDEBUG)
    {
    qDebug() << "QSCC::isConfigFile - Config file exists";
    }
    return true;
    }
    }@



  • Don't know whether this helps, but have you tried to call flush() on the text stream before closing the file? QTextStream calls flush on destruction but then you already closed the file. Not that the text you write is still in the buffer and has not yet been written when closing the file.



  • [quote author="PeerS" date="1394474435"]Don't know whether this helps, but have you tried to call flush() on the text stream before closing the file? QTextStream calls flush on destruction but then you already closed the file. Not that the text you write is still in the buffer and has not yet been written when closing the file.[/quote]

    Thanks - just tried it, but does not seem change anything.

    Bloody frustrating.



  • Well, if I take your code and make it into a runable app it works for me. As long as the config file does not exist it will be created. Afterwards, after it exist it gets used and read from. After deleting it it will be created again.
    Anyway, the question also is why don't you use QSettings?

    @
    #include <QCoreApplication>
    #include <QStandardPaths>
    #include <QDir>
    #include <QTextStream>
    #include <QDebug>
    #include <iostream>

    class Test
    {
    public:
    Test()
    {
    QString homeLocation = QStandardPaths::locate(QStandardPaths::HomeLocation, QString(), StandardPaths::LocateDirectory);

    this->configBasePath = homeLocation+".config/";
    this->configFolderPath = homeLocation+".config/QSaver/";
    this->configfilePath = homeLocation+".config/QSaver/QSaver.conf";
    this->defaultIdleTimeout = 5;
    

    }

    int createNewConfigFile()
    {
    QDir myDir(this->configFolderPath);

    if(!myDir.exists())
    {
      myDir.mkpath(this->configFolderPath);
      if(!myDir.exists())
      {
        qDebug() << "Error creating folder >" << this->configFolderPath << "<.";
        return 1;
      }
      qDebug() << "Folder >" << this->configFolderPath << "< created.";
    }
    
    QFile newfile&#40;this->configfilePath&#41;;
    
    if (newfile.open(QIODevice::WriteOnly | QIODevice::Text&#41;)
    {
      QTextStream stream(&newfile);
      stream << "<TIME>" << this->defaultIdleTimeout << "</TIME>" << endl;
      newfile.close();
      qDebug() << "File >" << this->configfilePath << "< created.";
    }
    else
    {
      qDebug() << "Error creating file >" << this->configfilePath << "<.";
      return 2;
    }
    
    return 0;
    

    }

    // Does the config file exist ?
    bool isConfigFile()
    {
    QFileInfo myFile(this->configfilePath);

    if(myFile.exists(&#41; == false)
    {
      return false;
    }
    else
    {
      return true;
    }
    

    }

    void readConfigFile()
    {
    QFile confFile(this->configfilePath);
    if (confFile.open(QFile::ReadOnly | QFile::Text))
    {
    std::cout << "Read: " << QString(confFile.readAll()).toStdString() << std::endl;
    }
    else
    {
    qDebug() << "Error: Unable to open file: " << this->configfilePath;
    }
    }

    private:
    QString configBasePath;
    QString configFolderPath;
    QString configfilePath;
    int defaultIdleTimeout;
    };

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

    Test t;
    if (!t.isConfigFile())
    {
    t.createNewConfigFile();
    }
    else
    {
    t.readConfigFile();
    }

    return a.exec();
    }
    @



  • Regarding QSettings, I thought it was easier as I only wanted to save one item - but I guess I'm wrong again. I think I will take a look at it.

    Much appreciation to both of you for helping :o)



  • Seems to have been a problem with the SDK installation.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.