-
September 20th, 2010, 11:42 AM
#1
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();
}
-
September 20th, 2010, 12:18 PM
#2
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.
-
September 20th, 2010, 12:33 PM
#3
Re: Create read-only file using c++
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
-
September 20th, 2010, 12:23 PM
#4
Re: Create read-only file using c++
Simple put this line..
system ("attrib +r logfileBrandNew.log");
or use SetFileAttributes API..
-
September 20th, 2010, 12:24 PM
#5
Re: Create read-only file using c++
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.
-
September 20th, 2010, 12:31 PM
#6
Re: Create read-only file using c++
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
-
September 20th, 2010, 12:47 PM
#7
Re: Create read-only file using c++
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.
-
September 20th, 2010, 12:56 PM
#8
Re: Create read-only file using c++
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
-
September 20th, 2010, 01:06 PM
#9
Re: Create read-only file using c++
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.
-
September 20th, 2010, 01:25 PM
#10
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.
-
September 20th, 2010, 01:27 PM
#11
Re: Create read-only file using c++
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.
-
September 20th, 2010, 01:36 PM
#12
Re: Create read-only file using c++
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 .......
-
September 20th, 2010, 01:37 PM
#13
Re: Create read-only file using c++
Originally Posted by neoraghav
thank you very much......your suggesstion has solved my problem as of now .......
How?
-
September 20th, 2010, 01:48 PM
#14
Re: Create read-only file using c++
Originally Posted by GCDEF
How?
bcoz......other concepts are not getting into my mind.......
so ill go with just changing the attribute flag ......as needed....
-
September 20th, 2010, 01:49 PM
#15
Re: Create read-only file using c++
Originally Posted by neoraghav
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|