Question about reading files in C++

Discussion in 'Programmer's Corner' started by moslem, Jul 2, 2010.

  1. moslem

    Thread Starter New Member

    Dec 16, 2009
    i want to know how can i handle the file changing when the file is opened and i'm reading from it or make any operation on it.
  2. Harrington

    New Member

    Dec 19, 2009
  3. someonesdad

    Senior Member

    Jul 7, 2009
    I suggest you read up on the concept of file locking. I haven't played with it in 10-15 years, so I don't remember much. But, IIRC, on UNIX, it was a "suggested" behavior that applications pay attention to file locks, but not required. Thus, the technique may or may not work for you. You didn't bother saying what OS you're using and these things can be very OS specific. A Windows programmer will have to speak up about Windows techniques, as I don't do Windows.

    Another technique, if you have appropriate permissions, is to lock out other users from accessing the file by changing permissions. This may or may not be usable. Another trick is to rename the file while you're using it, then restore the old name. But you have to be careful about problems interrupting this process -- you have to be damn sure that your program always renames the file no matter what happens. And this exception handling type stuff isn't easy for beginning programmers.