Thread: Stop program from running twice?

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    I like bean66's suggestion, but after googleing a little I feel it's a bit above my level. Therefore I'll try to stick with keeping the file open throughout the program. If I can't get that to work then I guess I'll have to look into locking the file...

    I am performing both reading and writing operations on the file frequently, but I need to keep the file open to restrict its access. Am I right in thinking that freopen() would do the job perfectly?
    I might not be a pro, but I'm usually right

  2. #17
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by bean66 View Post
    For file based locking:

    1. use a seperate file to control locking! Not the same file used for I/O.
    2. See fcntl (fd, F_SETLK, flock)
    3. use type = F_WRLCK
    Windows has fcntl()?

    EDIT: Also, if you're going to lock a secondary file, what's wrong with the old school method of simply opening in O_RDWR | O_EXCL | O_CREAT, then unlinking it when done?

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The C function fopen will deny read/write access by default.
    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.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Quote Originally Posted by Elysia View Post
    The C function fopen will deny read/write access by default.
    Yes I know that. But since I don't want the file to be accessible at all while the program is running, I should keep it open (whether reading or writing mode) and switch between the modes by using freopen(). something like this:
    Code:
    fopen(file, "r"); // first time
    ... // read from file and keep file open
    freopen(NULL, "w", file); // switch to write mode
    ... // write to file
    fclose();
    Would this work (I already started implementing it, but I'm still not sure it'll work).
    I might not be a pro, but I'm usually right

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's possible to open for read & write using "r+" or "w+".
    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. Stop Program
    By peckitt99 in forum Windows Programming
    Replies: 6
    Last Post: 11-13-2007, 10:22 AM
  2. Running a program
    By BlueVoid in forum C Programming
    Replies: 7
    Last Post: 04-30-2005, 11:48 PM
  3. Determaining if a program is running
    By WebmasterMattD in forum Linux Programming
    Replies: 4
    Last Post: 04-09-2002, 04:36 PM
  4. Running program on background?
    By Couhilin in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2001, 07:50 AM
  5. Why is my program running away?
    By badkitty in forum C++ Programming
    Replies: 4
    Last Post: 09-19-2001, 04:27 PM