Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to find a file is already opened or not?

How to find a file is already opened or not?

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
7 Posts 5 Posters 0 Views 1 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.
  • P Offline
    P Offline
    Paulraj G
    wrote on last edited by
    #1

    Hi Everybody, How to find a text file is already opened or not? I have used the following code. but it is not working correctly.

    ifstream inFile;
    inFile.open("C:\\test.txt");
    if (!inFile)
    {
    AfxMessageBox("file is opened");
    }
    else
    {
    AfxMessageBox("file not opened");
    }

    can anyone help me to how to find a file opened or not? Thanks....

    G.Paulraj

    A L T R 5 Replies Last reply
    0
    • P Paulraj G

      Hi Everybody, How to find a text file is already opened or not? I have used the following code. but it is not working correctly.

      ifstream inFile;
      inFile.open("C:\\test.txt");
      if (!inFile)
      {
      AfxMessageBox("file is opened");
      }
      else
      {
      AfxMessageBox("file not opened");
      }

      can anyone help me to how to find a file opened or not? Thanks....

      G.Paulraj

      A Offline
      A Offline
      Aescleal
      wrote on last edited by
      #2

      It all depends on how you opened the file. There's no reason why you can't have two streams based on the same file. e.g. on my system:

      int main()
      {
      std::ifstream str1( "c:\\test.txt" );
      std::ifstream str2( "c:\\test.txt" );

      int n = 0;
      int m = 0;
      
      if( ( str1 >> n ) && ( str2 >> m ) )
      {
      	std::cout << "Woo hoo! Opened both!" << std::endl;
      }
      

      }

      Always comes up with the "Woo hoo!" message and both n and m are set to the integer that's in the first token of the file. Unfortunately there's no way of opening a file using the standard library that locks it against further reading so if you're using streams you're not going to be able to tell easily if someone else has got it open. Cheers, Ash

      1 Reply Last reply
      0
      • P Paulraj G

        Hi Everybody, How to find a text file is already opened or not? I have used the following code. but it is not working correctly.

        ifstream inFile;
        inFile.open("C:\\test.txt");
        if (!inFile)
        {
        AfxMessageBox("file is opened");
        }
        else
        {
        AfxMessageBox("file not opened");
        }

        can anyone help me to how to find a file opened or not? Thanks....

        G.Paulraj

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Paulraj G wrote:

        I have used the following code. but it is not working correctly.

        What do you mean by not working? Your example shows an open of a single file with no sharing restriction. Please explain what you are trying to do and what results you receive.

        It's time for a new signature.

        1 Reply Last reply
        0
        • P Paulraj G

          Hi Everybody, How to find a text file is already opened or not? I have used the following code. but it is not working correctly.

          ifstream inFile;
          inFile.open("C:\\test.txt");
          if (!inFile)
          {
          AfxMessageBox("file is opened");
          }
          else
          {
          AfxMessageBox("file not opened");
          }

          can anyone help me to how to find a file opened or not? Thanks....

          G.Paulraj

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Paulraj G wrote:

          How to find a text file is already opened or not?

          Try opening the file in exclusive mode... filebuf::sh_none If you are unable to open the file exclusively then it means another application has an open handle. Best Wishes, -David Delaune

          A 1 Reply Last reply
          0
          • L Lost User

            Paulraj G wrote:

            How to find a text file is already opened or not?

            Try opening the file in exclusive mode... filebuf::sh_none If you are unable to open the file exclusively then it means another application has an open handle. Best Wishes, -David Delaune

            A Offline
            A Offline
            Aescleal
            wrote on last edited by
            #5

            Quick caveat here: filebuf::sh_none is not standard C++. You might end up with problems if you need to compile your code on another compiler. Cheers, Ash

            1 Reply Last reply
            0
            • P Paulraj G

              Hi Everybody, How to find a text file is already opened or not? I have used the following code. but it is not working correctly.

              ifstream inFile;
              inFile.open("C:\\test.txt");
              if (!inFile)
              {
              AfxMessageBox("file is opened");
              }
              else
              {
              AfxMessageBox("file not opened");
              }

              can anyone help me to how to find a file opened or not? Thanks....

              G.Paulraj

              T Offline
              T Offline
              ThatsAlok
              wrote on last edited by
              #6

              Paulraj G wrote:

              How to find a text file is already opened or not?

              It quite difficult, try to open file in write and sharedenyall , if it fails that means somebody else already open the file otherwise none. Preconditon: file must exists at stated path

              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
              Never mind - my own stupidity is the source of every "problem" - Mixture

              cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

              1 Reply Last reply
              0
              • P Paulraj G

                Hi Everybody, How to find a text file is already opened or not? I have used the following code. but it is not working correctly.

                ifstream inFile;
                inFile.open("C:\\test.txt");
                if (!inFile)
                {
                AfxMessageBox("file is opened");
                }
                else
                {
                AfxMessageBox("file not opened");
                }

                can anyone help me to how to find a file opened or not? Thanks....

                G.Paulraj

                R Offline
                R Offline
                rp_suman
                wrote on last edited by
                #7

                You can use CreateFile Function[] with "OPEN_EXISTING" flag in 5th argument(__in DWORD dwCreationDisposition) and use GetLastError Function to determine f the file opened already or not. Read complete documentation for the above.

                -- "Programming is an art that fights back!"

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups