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. Is it possible to open file twice?

Is it possible to open file twice?

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 4 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.
  • U Offline
    U Offline
    User 4396598
    wrote on last edited by
    #1

    Hi All, Is it possible to open the file twice? I am using CStdioFile, i created two objects of type CStdioFile and assigning the same file for both the objects. one file opened for writing(modeReadWrite) the data and the other file opened for reading(modeRead) the data. In this case my application is crashing. please give me some solution. CStdioFile obj1; CStdioFile obj2; obj1.Open("file1.txt", CStdioFile::modeReadWrite); ..... ..... ..... obj2.open("file1.txt", CStdioFile::modeRead); ..... ..... obj2.close(); .... ... obj1.close(); Thanks in advance! Regards, Anil

    C D R 3 Replies Last reply
    0
    • U User 4396598

      Hi All, Is it possible to open the file twice? I am using CStdioFile, i created two objects of type CStdioFile and assigning the same file for both the objects. one file opened for writing(modeReadWrite) the data and the other file opened for reading(modeRead) the data. In this case my application is crashing. please give me some solution. CStdioFile obj1; CStdioFile obj2; obj1.Open("file1.txt", CStdioFile::modeReadWrite); ..... ..... ..... obj2.open("file1.txt", CStdioFile::modeRead); ..... ..... obj2.close(); .... ... obj1.close(); Thanks in advance! Regards, Anil

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Why do you want to do such a thing ? Why don't you simply open and read the full file in memory, make the changes in memory and then write back everything to the file afterwards ?

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      U 1 Reply Last reply
      0
      • C Cedric Moonen

        Why do you want to do such a thing ? Why don't you simply open and read the full file in memory, make the changes in memory and then write back everything to the file afterwards ?

        Cédric Moonen Software developer
        Charting control [v3.0] OpenGL game tutorial in C++

        U Offline
        U Offline
        User 4396598
        wrote on last edited by
        #3

        yes, thankyou!

        1 Reply Last reply
        0
        • U User 4396598

          Hi All, Is it possible to open the file twice? I am using CStdioFile, i created two objects of type CStdioFile and assigning the same file for both the objects. one file opened for writing(modeReadWrite) the data and the other file opened for reading(modeRead) the data. In this case my application is crashing. please give me some solution. CStdioFile obj1; CStdioFile obj2; obj1.Open("file1.txt", CStdioFile::modeReadWrite); ..... ..... ..... obj2.open("file1.txt", CStdioFile::modeRead); ..... ..... obj2.close(); .... ... obj1.close(); Thanks in advance! Regards, Anil

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Member 4399771 wrote:

          In this case my application is crashing.

          At what point?

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Man who follows car will be exhausted." - Confucius

          1 Reply Last reply
          0
          • U User 4396598

            Hi All, Is it possible to open the file twice? I am using CStdioFile, i created two objects of type CStdioFile and assigning the same file for both the objects. one file opened for writing(modeReadWrite) the data and the other file opened for reading(modeRead) the data. In this case my application is crashing. please give me some solution. CStdioFile obj1; CStdioFile obj2; obj1.Open("file1.txt", CStdioFile::modeReadWrite); ..... ..... ..... obj2.open("file1.txt", CStdioFile::modeRead); ..... ..... obj2.close(); .... ... obj1.close(); Thanks in advance! Regards, Anil

            R Offline
            R Offline
            Rozis
            wrote on last edited by
            #5

            Yes you can, simply use 2 handles (or objects or whatever your language wants you to use). Be aware of the folloing: - The OS sees this as file sharing, so open in shared mode. - You will have 2 independent filepointers. Rozis

            R 1 Reply Last reply
            0
            • R Rozis

              Yes you can, simply use 2 handles (or objects or whatever your language wants you to use). Be aware of the folloing: - The OS sees this as file sharing, so open in shared mode. - You will have 2 independent filepointers. Rozis

              R Offline
              R Offline
              Rozis
              wrote on last edited by
              #6

              To Cedric and David: There are many cases opening a file twice can be handy. The simplest example is a Word document on a network. The first user reads it shared, read-write, the next shared, readonly. Be aware many DBMS use this for concurrent access (with some intelligence around). Rozis

              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