PDA

View Full Version : Writing text file in execlusive mode



viglu
1st April 2011, 21:15
Hello I want to write a file in Linux/Windows and in the same time of writing I want that nobody can open the file for reading and for writing.


QFile file(filename);
file.open(QIODevice::WriteOnly);
....
file.close();

With QIODevice::WriteOnly I can open the file with gEdit is that normal?

SixDegrees
1st April 2011, 21:20
Since you executed the program, any files created will default to being owned by you. You have permission to read and write your own files. Users tend to get very upset otherwise, when programs litter their system with files that can't be deleted.

Note that QFile allows you to set a wide range of permissions.

viglu
1st April 2011, 21:48
But also setting permissions will not help me.
I launch my qt program which write the file and I can also open it with an other program.

In Windows you can create a file with execlusiv rights, that's what I want to do with QT

SixDegrees
1st April 2011, 21:55
I don't know what 'exclusive rights' are, but someone - usually the owner, sometimes others - will always be able to read/write a file they've created. Any program that creates files in my user space that I am unable to manipulate is a program that will be headed to the trash heap in an instant.

If you can't set file permissions (and that's what they are) to what you'd like them to be using QFile, make a system call to the Windows API.

wysota
1st April 2011, 23:17
I think he means a mandatory lock. That's possible for Windows but not for Linux since locking is not mandatory there on most filesystems.

http://doc.trolltech.com/solutions/4/qtlockedfile/qtlockedfile.html