1

I have a program which opens the same file several times.
I want to check before open any file if this file currently is open or not because I don't want to open the same file several times.

enter image description here

Is there a built-in function which can check if the file is currently open or any other way can do that?

The Code:

QString openFilePath = QFileDialog::getOpenFileName(this->mainWindow, "Open File");
if(openFilePath == ""){
    return;
}
QFile openFile(openFilePath);
if(!openFile.open(QFile::ReadWrite)){
    QMessageBox::critical(this->mainWindow, "Can't Open file", "Can't access to the file.");
}
QTextStream fileContent(&openFile);
QFileInfo fileInfo(openFile);
this->createEmptyFile(fileInfo.fileName());
this->txtEditor->setText(fileContent.readAll());
Lion King
  • 32,851
  • 25
  • 81
  • 143

4 Answers4

6

It seems to me that your question has really nothing to do with file opening in the programmatic sense, but is exclusively related to your application logic. You need to internally keep a list of all currently open files (in the sense that your GUI is showing such file), and do a check if the user opens a new file.

Emerald Weapon
  • 2,392
  • 18
  • 29
0

Regular file open, say for append, depending on the function, will usually return NULL or raise an exception if the file is already open. There are stateless file systems where this approach may not work.

std::fstream fs;
try {
    fs.open("lk.txt", std::fstream::in | std::fstream::out | std::fstream::app);
    fs << "We're way beyond the boundaries of the Pride Lands.";
} catch (const std::ios_base::failure &ex) {
    // Something happened
    std::cerr << ex.what() << std::endl;
}
fs.close();

In some file systems there are also shared open modes, which will explicitly let you concurrently reopen the file and do what you want with no errors generated.

Yimin Rong
  • 1,890
  • 4
  • 31
  • 48
  • "will usually return NULL or raise an exception if the file is already open" this behavior depends on OS. – Slava Apr 21 '17 at 13:46
0

existing question

also you can try the hack) I dont know would it works or not, but: in the

void QFile::setFileName(const QString &name)

function overview QFile you can see that

Do not call this function if the file has already been opened.

Hmm) what if try to rename it in try catch to avoid crashing and if the renaming done, rename it again and open?) so you can try.

Community
  • 1
  • 1
Amir Rasulov
  • 131
  • 7
  • 1
    Not sure but I would expect that the docs refer to that you should not call `setFileName` on a `QFile` instance that was opened before, but I dont think there will be any exception or problem if you use two distinct `QFile` instances to open the same file twice – 463035818_is_not_an_ai Apr 21 '17 at 14:16
  • @tobi303 I didn't tested it yet, but I can hear the logic in your words) Seems it is works as you say – Amir Rasulov Apr 24 '17 at 11:52
-1

here is the solution to your problem:-

is_open()

Scope: std::ofstream::is_open

1.it Check if file is open or not?

2.it returns whether the stream is currently associated to a file

3.it is public member function of fstream.

4.parameters -none

Sample Code:

    #include <iostream>    
    #include <fstream>      // std::ofstream

    int main () 
    {
      std::ofstream ofs;
      ofs.open ("hye.txt");
      if (ofs.is_open())
      {
        ofs << "hellow world";
        cout << "successfully written to file";
        ofs.close();
      }
      else
      {
        cout << "Error opening file";
      }
      return 0;
    }

output:

successfully written to file

Shivam Sharma
  • 1,015
  • 11
  • 19
  • 1
    this works only if OP is using one and the same `ofsream` to open the same file twice, but I am pretty sure that OP is asking for opening the same file with two completely unrelated streams – 463035818_is_not_an_ai Apr 21 '17 at 14:12