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. Permissions and fopen

Permissions and fopen

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

    I'm using a call to fopen with the permissions of "a+" and writing logging information to the opened file. Unfortunately I'm now having to scale this to multiple programs and users. Is there some other permissions or even a different open file call that I can use that will ensure only one writer to the file and block any other opens? It seems that my current method allows multiple writers at the same time with the result that logging information is lost. Thanks. Chris Meech I am Canadian. [heard in a local bar] Remember that in Texas, Gun Control is hitting what you aim at. [Richard Stringer] Nice sig! [Tim Deveaux on Matt Newman's sig with a quote from me]

    V 1 Reply Last reply
    0
    • C Chris Meech

      I'm using a call to fopen with the permissions of "a+" and writing logging information to the opened file. Unfortunately I'm now having to scale this to multiple programs and users. Is there some other permissions or even a different open file call that I can use that will ensure only one writer to the file and block any other opens? It seems that my current method allows multiple writers at the same time with the result that logging information is lost. Thanks. Chris Meech I am Canadian. [heard in a local bar] Remember that in Texas, Gun Control is hitting what you aim at. [Richard Stringer] Nice sig! [Tim Deveaux on Matt Newman's sig with a quote from me]

      V Offline
      V Offline
      vipinasda
      wrote on last edited by
      #2

      Firstly, if you are programming in MFC, please use CFile, instead of low level calls. You had like to be free of having to track resource handles and explicitly calling fclose(...) for every fopen. With CFile you can use the CFile::shareExclusive to accomplish what you would want. At API level you could pass the 3rd parameter to CreateFile(...) as 0, it is actually the dwShareMode. If you plan to use the CRT routines, still , fo ahead and have a look at _locking(...)

      C 1 Reply Last reply
      0
      • V vipinasda

        Firstly, if you are programming in MFC, please use CFile, instead of low level calls. You had like to be free of having to track resource handles and explicitly calling fclose(...) for every fopen. With CFile you can use the CFile::shareExclusive to accomplish what you would want. At API level you could pass the 3rd parameter to CreateFile(...) as 0, it is actually the dwShareMode. If you plan to use the CRT routines, still , fo ahead and have a look at _locking(...)

        C Offline
        C Offline
        Chris Meech
        wrote on last edited by
        #3

        Thanks for the tips. Converting to a CFile looks to be the way to go. The fopen calls are from 10 year old library code that's never been touched. :-O Chris Meech I am Canadian. [heard in a local bar] Remember that in Texas, Gun Control is hitting what you aim at. [Richard Stringer] Nice sig! [Tim Deveaux on Matt Newman's sig with a quote from me]

        _ 1 Reply Last reply
        0
        • C Chris Meech

          Thanks for the tips. Converting to a CFile looks to be the way to go. The fopen calls are from 10 year old library code that's never been touched. :-O Chris Meech I am Canadian. [heard in a local bar] Remember that in Texas, Gun Control is hitting what you aim at. [Richard Stringer] Nice sig! [Tim Deveaux on Matt Newman's sig with a quote from me]

          _ Offline
          _ Offline
          __yb
          wrote on last edited by
          #4

          it wont help the problem is not your fopen but the multiple process access. u can several things. here are 2: 1. lock on a mutex with a name derived from the file's name. 2. if u can't open, Sleep for 10ms and retry repeatedly if u just enable other processes to touch the file while u write it, u will very quickly garble the file btw - in my openion fopen is much better than CFile

          V 1 Reply Last reply
          0
          • _ __yb

            it wont help the problem is not your fopen but the multiple process access. u can several things. here are 2: 1. lock on a mutex with a name derived from the file's name. 2. if u can't open, Sleep for 10ms and retry repeatedly if u just enable other processes to touch the file while u write it, u will very quickly garble the file btw - in my openion fopen is much better than CFile

            V Offline
            V Offline
            vipinasda
            wrote on last edited by
            #5

            hmm , then what are those flags built in the operating system for? They are just for that. Don't reinvent the wheel with mutexes and make the code a mess. Vipin - MVP

            _ 1 Reply Last reply
            0
            • V vipinasda

              hmm , then what are those flags built in the operating system for? They are just for that. Don't reinvent the wheel with mutexes and make the code a mess. Vipin - MVP

              _ Offline
              _ Offline
              __yb
              wrote on last edited by
              #6

              You are wrong here. Sharing flags are supplied to answer any possible user's need in creating/opening any kind of stream(file/pipe/sock...) but nobody said you could trust the sharing flags to automatically synchronize the read/write operations. Thread synchronization is the user's responsibility and using a named mutex is very simple and generic solution. In cross-process scenario the solution must include a lock/spin on a global object so these are the options: 1. The file itself, that can be locked on opening, so the other task should spin on CreateFile till success. 2. Lock on a named mutex 3. Use some other shared info like the registry

              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