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.

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();
}

How to achieve this using c++ Files....

Recommended Answers

All 11 Replies

you want to up the flag read only or you just need to lock the file while your
application is running?

I suggest you to use a lock here.
For that I need more information , are you on the linux?

ifstream in;    // Create an input file stream.
in.open("data.txt");

that will open data.txt as read only. If you want to protect the file from being written over, I suggest you modify the properties of the file, or (if using windows) go to a command prompt, go to the folder with the file in it, and type

attrib +r filename

which will set the file itself to read only.

Making the file "read only" will NOT prevent an external application or the user from changing or deleting it. It mearly makes it more inconvenient. External applications or the user can always change the file permissions to read/write and either change the file's contents or delte it.

If you want to make it read-only only while your program has it open, that's a different story.

you want to up the flag read only or you just need to lock the file while your
application is running?

I suggest you to use a lock here.
For that I need more information , are you on the linux?

no. i'm on windows and using vc++ 6.0.I've been told to make a file read only as a part of my case study....and also the program can make change to the file but others cannot.

ifstream in;    // Create an input file stream.
in.open("data.txt");

that will open data.txt as read only. If you want to protect the file from being written over, I suggest you modify the properties of the file, or (if using windows) go to a command prompt, go to the folder with the file in it, and type

attrib +r filename

which will set the file itself to read only.

thank you dude.......it work wellin windows..

but i want to do this using c++ program......is there any way to do this...

Making the file "read only" will NOT prevent an external application or the user from changing or deleting it. It mearly makes it more inconvenient. External applications or the user can always change the file permissions to read/write and either change the file's contents or delte it.

If you want to make it read-only only while your program has it open, that's a different story.

okay....thanks for your suggestion...i agree with you
but is their a way to make a file read only using c++ code....i want to know about that..

thanks in advance

win32 api function SetFileAttributes()

dude, i'm creating win32 console application......
it's showing error...

system("attrib +r filename") will this work.....

thanks....

>> dude, i'm creating win32 console application......

Ancient Dragon's suggestion will work provided that you #include <windows.h> If you don't want to pull in the windows.h header, a more lightweight solution would be to use C run-time function _chmod(). To use that function, you need to #include <io.h> .

>> dude, i'm creating win32 console application......

Ancient Dragon's suggestion will work provided that you #include <windows.h> If you don't want to pull in the windows.h header, a more lightweight solution would be to use C run-time function _chmod(). To use that function, you need to #include <io.h> .

thank you mitrmkar.it's working.........
i'm yet to try with Ancient Dragon's suggestion . hope i'll work.

thank you......

win32 api function SetFileAttributes()

as mitrmkar said.i'll try SetFileAttributes with windows.h ...

thank you Ancient Dragon

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.