Thread: Making a file read only

  1. #1
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52

    Making a file read only

    How to make a file read only using C program??

    Is there a function called chmod and what is its use??

    What is meant by r+ ,w+ ,a+ modes in file?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since you appear to be possibly on the right track with your reference to chmod, I suggest that you search the Web for more information.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by linuxlover View Post
    Is there a function called chmod and what is its use??
    Yeah, POSIX provides this, but it's not portable to other OS's. Man pages can be Googled quite easily.

  4. #4
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52
    Quote Originally Posted by JohnGraham View Post
    Yeah, POSIX provides this, but it's not portable to other OS's. Man pages can be Googled quite easily.
    Any other way instead of using library functions????

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by linuxlover
    Any other way instead of using library functions?
    What exactly do you mean by that?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52
    Quote Originally Posted by laserlight View Post
    What exactly do you mean by that?
    ie. by using some code..

  7. #7
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    If you are in Windows and only Windows then I would use the Windows API. If you want portable code then use the CRT.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by linuxlover
    ie. by using some code..
    You write code that uses library functions. I don't see what else you are getting at here.

    Quote Originally Posted by Bubba
    If you want portable code then use the CRT.
    I do not think that the standard C library has support for this.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I do not think that the standard C library has support for this.
    Come to think of it I don't think it does either. See...this is why I should stay away from the C board.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This was part of the AT&T Unix standard, and incorporated into Turbo C. There was a more extensive alternative of this, that was for DOS only (named _chmod), and it may still work in Windows, but far safer to use the Windows API unless Microsoft has stated that they will continue to keep the old standard for this, in their OS.

    Code:
    chmod   Changes access mode.
     Syntax:
       int chmod(const char *path, int amode);
    
     Prototype in:
     io.h
    
     Remarks:
    chmod sets the file-access permissions of the file given by path according
    to the mask given by amode.
    
    path points to a string; *path is the first character of that string.
    
    amode can contain one or both of the symbolic constants S_IWRITE and S_IREAD
    (defined in sys\stat.h).
    
       Value of amode   ³ Access permission
       S_IWRITE         ³ Permission to write
       S_IREAD          ³ Permission to read
       S_IREAD|S_IWRITE ³ Permission to read and write
    
     Return Value:
    Upon successfully changing the file-access mode, chmod returns 0.
    
    Otherwise, chmod returns a value of -1 and the global variable errno is set
    to one of the following:
    
     Setting ³ Description
     ENOENT  ³ Path or file name not found
     EACCES  ³ Permission denied
    
     Portability:
    chmod is available on UNIX systems.
    
     See Also:
      _chmod
    
     Example:
     #include <sys\stat.h>
     #include <stdio.h>
     #include <io.h>
    
     void make_read_only(char *filename);
    
     int main(void)
     {
        make_read_only("NOTEXIST.FIL");
        make_read_only("MYFILE.FIL");
        return 0;
     }
    
     void make_read_only(char *filename)
     {
        int stat;
    
        stat = chmod(filename, S_IREAD);
        if (stat)
           printf("Couldn't make %s read-only\n", filename);
        else
           printf("Made %s read-only\n", filename);
     }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. How to convert char read from file into string
    By cruxxe in forum C Programming
    Replies: 7
    Last Post: 05-22-2002, 02:09 PM

Tags for this Thread