humm does it possible to prevent access for editing a text file?
I am talking about access for writing not about erasure of the file.
humm does it possible to prevent access for editing a text file?
I am talking about access for writing not about erasure of the file.
Yes, make the file read-only, and make sure the user that would want to delete the file is different from the file owner, and that the user hasn't got rights to change the files protection.
How you do that depends on which OS you are using, and of course has nothing (directly) to do with programming in C (although if you plan on doing this in C, you may want to tell us which OS it is you are using, and this post can be moved to the relevant OS discussion group).
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
To deny access to a single file, have it opened exclusively in your program.
So long as you don't close the file, no other applications can access the file.
(It isn't fool proof, because it's possible to terminate your program and get access to the file.)
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Maybe you both stop using acronyms? XD
Look I have a file with highscore (and name) and I don't want the users to change it manually so I must prevent access to the file (no matter if the game is running or not) or I should encrypte the score and the name (adding more rows) so I prefer not doing this.
And I am using XP (unfortunately).
gavra.
I don't think you need to be that ruthless, but if you want to protect it if not running your program, set it to read-only (will prevent modifying it) or use file permission security to prevent opening/modifying file (requires NTFS file system).
The former is easy. The latter is difficult.
Sorry, no can do. If your application can open the file, then the user can, because your application will be running under that users privileges - so anything your app can do, the user can do with another application (e.g. notepad).
Storing the data in an encrypted form, binary form or checksumming the information would reduce the likelyhood of anyone cheating this way.
Of course, the only one the user is cheating is him/herself unless this is a commercial game, so it's not, in my view, particularly important.
You could of course make the file read only and/or hidden (and change it's attribute when you are about to open the file, then change it back when you close the file), then it would require the user to undo the read-only bit before being able to save the file - but unlikely to prevent anyone with more than a couple of weeks experience with computers.
Here's the Windows API for SetFileAttribute
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
You can't prevent the user from accessing the file - you can make it more difficult to find and edit by hiding the file (so the user won't know it's there, just like you may or may not know that there is a boot.ini file in your C:\ directory - if you didn't know it was there, you wouldn't try to modify it, right?). Or you can make the file read-only - then the user can see the file, but won't be able to edit it without changing it's attributes.
Neither would stop me from modifying the file if I wished to do so, since I know how to unhide a file and also how to un-read-only a file (and no, I'm not talking about any clever tricks, just tools that are delivered with the Windows XP installation by default, such as notepad and widnows explorer or the attrib.exe that is available from command line). Anyone reasonably familiar with Windows would be able to bypass this sort of thing.
To more thoroughly prevent modification, you could add a checksum that is at the end of the file - if the checksum of the file content doesn't match the stored checksum, then the file is rejected. This means that the user would have to figure out how to calculate the chosen checksum - which may be quite tricky if the user doesn't even know what the checksum calculation is, and even if it's known, the user would have to be able to write a program of some sort that can regenerate the checksum. CRC-32, MD5sum are two potential candidates here.
Or you can encrypt the entire file content.
Or you could store the data not in a text file, but as binary. That will prevent MOST people from messing with the file, because you can't just load it in a text editor and edit it, but it wouldn't stop me for very long, as I know how to use a hex editor (or write a small program that reads/writes the relevant byte(s) of a binary file).
An additional trick would be to pad the actual data with some other garbage, e.g. random numbers to hide where the actual data is being held. That way, it makes the finding out what is the valid data in the file.
Storing the data in a "disorganized fashion" (e.g. you first copy your data to a large array, then use rand() with a set srand() seed to "jumble" the data - since you use a set srand() seed [or store the seed in the data file], you are guaranteed to get the same random number sequence next time around, so you can restore the data into correct order.
To make this more efficient, you may want to store some more random garbage in the file, so that it's always a fixed size [or a multiple of a fixed block size].
You can of course combine any and all of the above. And encrypted, checksummed, binary content (using sufficiently strong encryption) would stop most people from tampering with the file. Encrypted files are quite difficult to deal with as you need to first decrypt the encryption to be able to figure out what and where the content is held.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Users can range from Genius's to total morons.
The morons won't even know what a file is, let alone how to change it; but the genius's will always be able to find a way (if they actually care enough about it to waste their time trying).
The best you can do is encrypt the file to prevent 99% of people from modifying it.
First option - how can I hide the file?
Second - how can I make the file for read only?
"disorganized fashion" - I thought about this way and maybe I'll implement this way.
Binary seems to be cool.
Now I have so many options so I have to think over..
thank you all specially for you mats.
gavra.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Yep I have seen it but I keep asking my self "WTF?"
Sorry it's just I didn't understand where is the function and how do I use it..
gavra.