Thread: Two programmings trying to access the same file

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    Two programmings trying to access the same file

    I have a situation where I will have two different programs possibly trying to access/modify a single file at potentially the same time. There is no way around having two separate programs, so that is not debatable. Will I get some sort of error code in the program I am writing in C++ if it tries to open the file, and the other program is still using/modifying it? Can I create a loop that can read that error code, and keep trying to access it until the other program is done with it?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to "lock" the file, access it, then release the lock. For windows, see LockFile (or LockFileEx if you need more functionality).

    Unix will have something similar.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    2
    Quote Originally Posted by oogabooga View Post
    You need to "lock" the file, access it, then release the lock. For windows, see LockFile (or LockFileEx if you need more functionality).

    Unix will have something similar.
    What will happen if I do not lock it? Will it just grab the part of the file that is completed while the first program proceeds to complete the rest? I was thinking about making a special end of file character string so the C++ program reading the file would know if the file is completed by the other program or not if that is the case.

    Also, what will happen if the other program tried to open the locked file?

    Forgive me for my noviceness, it has been nearly 10 years since I did any C++ programming, and none of it involved opening files etc.

    I guess a little background would be in order. I am using a scripting program to turn emails into .txt files as soon as they arrive on my server. The C++ program then loads the contents of the txt files into a string asap (both the script and the C++ program will be constantly running on the server) and does stuff with the strings within the program. So, the C++ program will never modify the .txt file, it will only read it and load it into a string.
    Last edited by Chris Harrigan; 02-06-2012 at 05:35 PM.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    What will happen if I do not lock it? Will it just grab the part of the file that is completed while the first program proceeds to complete the rest?
    Maybe. Try it and see. It may not see any of the file even if part of it has been "written" since output is presumably buffered (kept in ram until a certain number of bytes have accumulated or until the file is "flushed" upon closing).

    Also, what will happen if the other program tried to open the locked file?
    It depends on the type of lock. When either program wants to access the file, it first tries to lock it. If it can't lock it (because it is already locked by the other program), then it waits and tries again. LockFileEx (with the right flags, and again assuming Windows) will automatically wait until it can get the lock.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Windows will typically lock a file that is opened by another process without the need for any api calls. So try it. If it doesn't work, then you can manually try locking it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 10-14-2011, 01:31 PM
  2. Replies: 2
    Last Post: 07-18-2009, 01:17 PM
  3. File I/O (Reading from a Random-Access File)
    By VersEtreOuNe in forum C++ Programming
    Replies: 25
    Last Post: 02-14-2008, 05:13 AM
  4. How to convert access file to excel file????
    By husna in forum Windows Programming
    Replies: 2
    Last Post: 09-03-2001, 09:59 PM