Thread: text files

  1. #1
    Registered User
    Join Date
    Jul 2008
    Location
    Fort Worth TX
    Posts
    15

    text files

    I was curious how to lock a text file and unlock it?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You can't do that in standard C. Such a task requires something platform specific.

  3. #3
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    which OS?
    gavra.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Location
    Fort Worth TX
    Posts
    15
    Linux Slackware.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    What do you mean 'lock' ?

    Make it so only you and the admins can read it?
    Just do this at your command prompt,
    chmod 400 myfile.txt
    and it will be readable by you only.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    Multiple ways but look at these before attempting:
    1. http://linux.die.net/man/2/open
    2. http://linux.die.net/man/2/flock
    3. http://linux.die.net/man/2/fcntl (more complicated)

    Some simplified steps are:
    1. open() the file so you have a file descriptor to pass to flock.
    2. now apply the lock using flock()
    3. unlock with flock()
    4. close() the file descriptor.

    Also, keep in mind that locks pertain only to other programs following the locking mechanism.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Reading text files?
    By Kate in forum C Programming
    Replies: 3
    Last Post: 06-30-2006, 01:22 AM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. reading certain parts of text files
    By Captain Penguin in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2002, 09:45 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM