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.
  • RIVOPICOR Offline
    RIVOPICOR Offline
    RIVOPICO
    wrote on last edited by A Former User
    #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 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");
      
      Pradeep KumarP 1 Reply Last reply
      7
      • Pradeep KumarP Offline
        Pradeep KumarP Offline
        Pradeep Kumar
        wrote on 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

          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");
          
          Pradeep KumarP Offline
          Pradeep KumarP Offline
          Pradeep Kumar
          wrote on 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 last edited by
            #5

            Another way:

            qDebug() << QDir("/home/pw/docs").exists("file.txt");
            
            Pradeep KumarP 1 Reply Last reply
            4
            • ? A Former User

              Another way:

              qDebug() << QDir("/home/pw/docs").exists("file.txt");
              
              Pradeep KumarP Offline
              Pradeep KumarP Offline
              Pradeep Kumar
              wrote on 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

              • Login

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