Can't create a .txt file in specified directory or mkdir()



  • I want to create a main.txt file in "data" sub-directory under current working directory and mkdir() can't work.Here is my code.

     QString currentWorkingPath=QCoreApplication::applicationFilePath();
     QDir dir(currentWorkingPath);
     qDebug()<<dir.currentPath();//It output the correct path until here
    
     if(dir.mkdir("data"))
      qDebug()<<"MAKE THE DIR";//It can't mkdir() here.
    
        QFile mainFile(QString(currentWorkingPath+"/data/main.txt"));//It can't create new sub-dir or txt file either.
        if (mainFile.open(QIODevice::WriteOnly|QIODevice::Text)){
    
        }
        mainFile.close();
    

    Can someone give me some advice?I can only create txt in output home directory now.


  • Lifetime Qt Champion

    Hi,

    Ensure you are not trying to write in a read-only folder. On most OS, the path where an application is installed is not writable by users partly because I could wreak havoc on the device/machine. The QStandardPaths class allows to retrieve usable writable paths for your application to write into depending on the type of data you want to generate.



  • @SGaist Thx and I can use QStandardPath to get a writable path.


Log in to reply
 

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