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();
}
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.
Re: Create read-only file using c++
Simple put this line..
system ("attrib +r logfileBrandNew.log");
or use SetFileAttributes API..
Re: Create read-only file using c++
Quote:
Originally Posted by
hypheni
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.
Re: Create read-only file using c++
Quote:
Originally Posted by
hypheni
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
Re: Create read-only file using c++
Quote:
Originally Posted by
GCDEF
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
Re: Create read-only file using c++
Quote:
Originally Posted by
neoraghav
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.
Re: Create read-only file using c++
Quote:
Originally Posted by
Skizmo
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
Re: Create read-only file using c++
Quote:
Originally Posted by
neoraghav
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.
Re: Create read-only file using c++
Quote:
Originally Posted by
GCDEF
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....
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.
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.
Re: Create read-only file using c++
Quote:
Originally Posted by
hypheni
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.
Re: Create read-only file using c++
Quote:
Originally Posted by
Ankheg
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
Re: Create read-only file using c++
Quote:
Originally Posted by
hypheni
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 .......