Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't create a .txt file in specified directory or mkdir()
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MartinChan
    wrote on last edited by MartinChan
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      4
      • SGaistS SGaist

        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.

        M Offline
        M Offline
        MartinChan
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved