Create read-only file using c++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 23

Thread: Create read-only file using c++

Hybrid View

  1. #1
    Join Date
    Sep 2010
    Posts
    15

    Cool Create read-only file using c++

    Hi,
    I want to create a log file that captures all the actions happening...anyway i got code for that from internet.
    Now what i want to do is, make that file read only so that the data is not modified by external application or user.
    Code:
    log(char* msg)
    {   
        time_t now = time(0);   
        struct tm* tm = localtime(&now);       
        ofstream out( "logfileBrandNew.log",ios::app);
       
        out << tm->tm_year << '/' << tm->tm_mon << '/' << tm->tm_mday        
            << ' ' << tm->tm_hour << ':' << tm->tm_min << ':' << tm->tm_sec
            << ": ";   
        out << msg << "\n";   
        out.close();
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: Create read-only file using c++

    There's no real easy way to do that. If you really need it to be secure, your best bet is to encrypt it and use some kind of check sum to verify its integrity.

  3. #3
    Join Date
    Sep 2010
    Posts
    15

    Re: Create read-only file using c++

    Quote Originally Posted by GCDEF View Post
    There's no real easy way to do that. If you really need it to be secure, your best bet is to encrypt it and use some kind of check sum to verify its integrity.
    thank you......

    can you just mention some functions or methods to achieve this....

    thank you

  4. #4
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Create read-only file using c++

    Simple put this line..

    system ("attrib +r logfileBrandNew.log");

    or use SetFileAttributes API..

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: Create read-only file using c++

    Quote Originally Posted by hypheni View Post
    Simple put this line..

    system ("attrib +r logfileBrandNew.log");

    or use SetFileAttributes API..
    And then all a user has to do is use explorer, turn off the read only attribute, and do whatever they want to the file. I could be wrong, but I think he needs something more secure than that.

  6. #6
    Join Date
    Sep 2010
    Posts
    15

    Re: Create read-only file using c++

    Quote Originally Posted by hypheni View Post
    Simple put this line..

    system ("attrib +r logfileBrandNew.log");

    or use SetFileAttributes API..
    thanks dude.....

    but this is restricting the program to modify or update log details.......
    how to solve this

  7. #7
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Create read-only file using c++

    Quote Originally Posted by neoraghav View Post
    thanks dude.....

    but this is restricting the program to modify or update log details.......
    how to solve this
    You could always keep the file open yourself so nobody can write to it, but as soon as your program closes, the lock is gone.

  8. #8
    Join Date
    Sep 2010
    Posts
    15

    Re: Create read-only file using c++

    Quote Originally Posted by Skizmo View Post
    You could always keep the file open yourself so nobody can write to it, but as soon as your program closes, the lock is gone.
    dude.....i want to achieve this using code man.......give me good suggestion

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: Create read-only file using c++

    Quote Originally Posted by neoraghav View Post
    dude.....i want to achieve this using code man.......give me good suggestion
    We don't really know what your requirements are and from your OP it seems you're just starting out. It's not a simple topic and you may be getting in over your head. Probably the simplest encryption is just to XOR a bit pattern over your data. There are various checksum algorithms you can use to help ensure the data hasn't been modified by someone other than you. You'll need to decide what your program does if the user deletes your file, or replaces it with an earlier version. Making it really secure won't be easy.

  10. #10
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Create read-only file using c++

    I think people making OP much confuser to find his and.. Do one simple thing..

    Make your file normal before reading it by SetFileAttributes (FileName, FILE_ATTRIBUTE_NORMAL) then just before closing your program call the same API with FILE_ATTRIBUTE_HIDDEN.

    I think there is no more confusion. Also do the same approach with system() with +r and -r.

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: Create read-only file using c++

    Quote Originally Posted by hypheni View Post
    I think people making OP much confuser to find his and.. Do one simple thing..

    Make your file normal before reading it by SetFileAttributes (FileName, FILE_ATTRIBUTE_NORMAL) then just before closing your program call the same API with FILE_ATTRIBUTE_HIDDEN.

    I think there is no more confusion. Also do the same approach with system() with +r and -r.
    As has been explained already, all a user has to do is turn off those attributes and they can do what they want with the file. That isn't secure.

  12. #12
    Join Date
    Sep 2010
    Posts
    15

    Re: Create read-only file using c++

    Quote Originally Posted by hypheni View Post
    I think people making OP much confuser to find his and.. Do one simple thing..

    Make your file normal before reading it by SetFileAttributes (FileName, FILE_ATTRIBUTE_NORMAL) then just before closing your program call the same API with FILE_ATTRIBUTE_HIDDEN.

    I think there is no more confusion. Also do the same approach with system() with +r and -r.
    thank you very much......your suggesstion has solved my problem as of now .......

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: Create read-only file using c++

    Quote Originally Posted by neoraghav View Post
    thank you very much......your suggesstion has solved my problem as of now .......
    How?

  14. #14
    Join Date
    Sep 2010
    Posts
    15

    Re: Create read-only file using c++

    Quote Originally Posted by GCDEF View Post
    How?
    bcoz......other concepts are not getting into my mind.......

    so ill go with just changing the attribute flag ......as needed....

  15. #15
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: Create read-only file using c++

    Quote Originally Posted by neoraghav View Post
    bcoz......other concepts are not getting into my mind.......

    so ill go with just changing the attribute flag ......as needed....
    The fact that you don't understand the solution doesn't change the fact that just changing the attributes does nothing to solve your problem.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center