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

Thread: Create read-only file using c++

  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
    12,090

    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
    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..

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,090

    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.

  5. #5
    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

  6. #6
    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

  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
    12,090

    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
    Sep 2010
    Posts
    15

    Re: Create read-only file using c++

    Quote Originally Posted by GCDEF View Post
    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.
    yes i'm a newbie ...

    i'll try with checksum algorithms as you mentioned.....

    thank you very much GCDEF....
    Last edited by neoraghav; September 20th, 2010 at 01:21 PM.

  11. #11
    Join Date
    Aug 2010
    Posts
    47

    Re: Create read-only file using c++

    Well, I'd say the real solution isn't one you're likely to want to use.

    The problem is that anything your program can do, another program can also do... if it's run by the same user with the same permissions. Simply put: if the person has control over the machine, it's their machine. They get to do what they want with it.

    So the solution is to limit access to your program to machines you control, and only give the user limited access to those machines. Basically you create a user account with limited privileges that cannot edit anything outside of their directory. You then let them use your program, and if they wanted to edit the log file they would then have to give an admin password, which they would not have.

    If you have to make it so that they cannot edit their logfiles on *their* machines when they have full access, then things get a lot harder. As someone suggested, I guess you can try encryption or something, but this probably isn't a very good solution. I guess it comes down to why you can't let them edit the log files.

    Personally, my advice would probably be to just let them.

  12. #12
    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.

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

    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.

  14. #14
    Join Date
    Sep 2010
    Posts
    15

    Re: Create read-only file using c++

    Quote Originally Posted by Ankheg View Post
    Well, I'd say the real solution isn't one you're likely to want to use.

    The problem is that anything your program can do, another program can also do... if it's run by the same user with the same permissions. Simply put: if the person has control over the machine, it's their machine. They get to do what they want with it.

    So the solution is to limit access to your program to machines you control, and only give the user limited access to those machines. Basically you create a user account with limited privileges that cannot edit anything outside of their directory. You then let them use your program, and if they wanted to edit the log file they would then have to give an admin password, which they would not have.

    If you have to make it so that they cannot edit their logfiles on *their* machines when they have full access, then things get a lot harder. As someone suggested, I guess you can try encryption or something, but this probably isn't a very good solution. I guess it comes down to why you can't let them edit the log files.

    Personally, my advice would probably be to just let them.
    yes...you're right
    i tried with some options and they didnt work well...
    but this "read only " thing is part of my case study where i have to capture all the actions using a log file and make that log file read only. i know that its lot complex and since i'm a newbie i'm not getting many things mentioned by some guys in other forums......

    anyway i'll go with using the checksum algorithm now.....

    thank you

  15. #15
    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 .......

Page 1 of 2 12 LastLast

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center