Creating a file in c:\



  • Hi,

    With the code below we ceate a file in the debug file.
    How can I create the file in C:\ ?
    Thanks in advance for your help.

    @//creating a file named dossier donnes- if it doesn't exist
    QDir lDir;
    if (not lDir.exists("dossier donnes")) lDir.mkdir("dossier donnes");
    @


  • Moderators

    Use a QFile to create files. Open the file, then write your data, then close the file. See http://qt-project.org/doc/qt-5.0/qtcore/qfile.html for more details

    @
    QFile output("myfile.txt");

    bool ok = output.open(QFile::WriteOnly|QFile::Text);
    if (ok)
    {
    output.write("Debug text");
    output.close();
    }
    else
    {
    qDebug("Could not open file!");
    }
    @



  • Hi,
    Thank you for your reply.
    I badly formulated my question. Sorry about it.
    But my question was not 'how to create a file' but 'how to create a directory file' at a certain location'
    debug is a directory file created by QTCreator when compiling a file . It contains beside others the .exe program.
    'dossier donnes' is a directory file I want to create in C:\ .
    And in this directory file (dossier donnes) I want to create in a next step, a txt file.


  • Moderators

    The constructor allows you to provide a starting directory, "see QDir.":http://qt-project.org/doc/qt-5.0/qtcore/qdir.html#QDir-2 Since you did not specify a string it assigns an empty one. I am not sure where this points, but you know already where to start, so
    @
    //creating a file named dossier donnes- if it doesn't exist
    QDir lDir ( "c:/" );
    if (not lDir.exists("dossier donnes")) lDir.mkdir("dossier donnes");
    @
    should work.

    Alternatively, you may use "mkpath":http://qt-project.org/doc/qt-5.0/qtcore/qdir.html#mkpath which allows to create cascading directories at once.


  • Moderators

    Sorry, just saw the "not" in the if statement, what compile language are you using?
    In C++ it would be

    @
    //creating a file named dossier donnes- if it doesn't exist
    QDir lDir ( "c:/" );
    if ( ! lDir.exists("dossier donnes")) lDir.mkdir("dossier donnes");
    @

    or

    @
    //creating a file named dossier donnes- if it doesn't exist
    QDir lDir ( "c:/" );
    if ( ! lDir.exists("c:/dossier donnes/dossier donnes")) lDir.mkpath("c:/dossier donnes/dossier donnes");
    @

    Note: you may use exists for absolute path already.



  • Thank you for your help.


Log in to reply
 

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