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. Check if file exists Qt
Forum Updated to NodeBB v4.3 + New Features

Check if file exists Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 54.5k 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.
  • R Offline
    R Offline
    RIVOPICO
    wrote on 7 Oct 2016, 20:56 last edited by A Former User 10 Jul 2016, 21:54
    #1

    hi i tried many ways but lately take me problems? some working example. sorry again..

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on 7 Oct 2016, 21:29 last edited by
      #2

      Hi!
      There are several ways to do that. One is using [static] bool QFile::exists(const QString &fileName), e.g.:

      qDebug() << QFile::exists("/home/pw/docs/file.txt");
      
      P 1 Reply Last reply 8 Oct 2016, 07:05
      7
      • P Offline
        P Offline
        Pradeep Kumar
        wrote on 8 Oct 2016, 06:59 last edited by
        #3

        Hi,

        You can also use QFileInfo Class to check the existance of file.

        Sample code.

        QString fileName("./sample.txt");
        QFile file(fileName);
        if(QFileInfo::exists(fileName))
        {
            qDebug () << "file exists" << endl;
            file.open(QIODevice::ReadWrite | QIODevice::Text);
            QString data =  file.readAll();
            qDebug () << "data in file:" << data << endl;
            qDebug()<<"file already created"<<endl;
            file.close();
        }
        else
        {
            qDebug () << "file does not exists" << endl;
            file.open(QIODevice::ReadWrite | QIODevice::Text);
            file.write("Qt Programming");
            qDebug()<<"file created"<<endl;
            file.close();
        }
        

        Checks files exists, if not exists, file gets created and data is written to the file.
        If file exists read the data from the file.

        Thanks,

        Pradeep Kumar
        Qt,QML Developer

        1 Reply Last reply
        4
        • ? A Former User
          7 Oct 2016, 21:29

          Hi!
          There are several ways to do that. One is using [static] bool QFile::exists(const QString &fileName), e.g.:

          qDebug() << QFile::exists("/home/pw/docs/file.txt");
          
          P Offline
          P Offline
          Pradeep Kumar
          wrote on 8 Oct 2016, 07:05 last edited by
          #4

          As @Wieland said many ways to check

          There are several ways to do that. One is using [static] bool QFile::exists(const QString &fileName), e.g.:

          qDebug() << QFile::exists("/home/pw/docs/file.txt");
          
          QString fileName("./sample.txt");
          QFile file(fileName);
          qDebug () << file.exists();
          

          exists() method with parameter and without parameter.

          Thanks,

          Pradeep Kumar
          Qt,QML Developer

          1 Reply Last reply
          2
          • ? Offline
            ? Offline
            A Former User
            wrote on 8 Oct 2016, 07:07 last edited by
            #5

            Another way:

            qDebug() << QDir("/home/pw/docs").exists("file.txt");
            
            P 1 Reply Last reply 8 Oct 2016, 07:08
            4
            • ? A Former User
              8 Oct 2016, 07:07

              Another way:

              qDebug() << QDir("/home/pw/docs").exists("file.txt");
              
              P Offline
              P Offline
              Pradeep Kumar
              wrote on 8 Oct 2016, 07:08 last edited by
              #6

              @Wieland said in Check if file exists Qt:

              Another way:

              qDebug() << QDir("/home/pw/docs").exists("file.txt");
              

              Nice Info.

              Pradeep Kumar
              Qt,QML Developer

              1 Reply Last reply
              3

              2/6

              7 Oct 2016, 21:29

              4 unread
              • Login

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