We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

file overwrtie

pbsmbc
pbsmbc asked
on
Medium Priority
259 Views
Last Modified: 2013-11-20
Hi,

While writing a file using the following function we are not able to overwrite an existing file which is read only.

std::ofstream outputFile(OutputLocation, std::ios::binary);
outputFile.write((const char*)lpResLock, dwSizeRes);
outputFile.close();

Can anyone help us to mdify the code so that we are able to overwrite the existing file irrespective
of the file mode (read only, ... etc)

Thanks in advance.
Comment
Watch Question

CERTIFIED EXPERT
Author of the Year 2009

Commented:
You can change the file permission settings before trying to write to the file:
       #include <sys/stat.h>
       #include <sys/stat.h>

        int nRet= _chmod( szPathAndNameOfTheFile, _S_IREAD | _S_IWRITE );
        if ( nRet != 0 ) {
                ... oops an error! ...
        }
AxterSenior Software Engineer

Commented:
Use SetFileAttributes API function before opening the file.
Senior Software Engineer
Commented:
SetFileAttributes(OutputLocation, FILE_ATTRIBUTE_ARCHIVE);

If you want to preserve the attributes, you can first read them, and save them for when you're done.
Example:

DWORD MyAttrib = GetFileAttributes(OutputLocation);
SetFileAttributes(OutputLocation, FILE_ATTRIBUTE_ARCHIVE);

std::ofstream outputFile(OutputLocation, std::ios::binary);
outputFile.write((const char*)lpResLock, dwSizeRes);
outputFile.close();

SetFileAttributes(OutputLocation, MyAttrib); //Set it back to original value.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks Axter. Its working fine.
I have aquextion.Why FILE_ATTRIBUTE_ARCHIVE attribute is removing read only setting of the file?
AxterSenior Software Engineer

Commented:
>>Why FILE_ATTRIBUTE_ARCHIVE attribute is removing read only setting of the file?

It doesn't remove it.
When you call SetFileAttributes, you have to include all the attributes you want on the file.  If you don't include the attribute, than it gets removed.
Since the example code I posted only includes the FILE_ATTRIBUTE_ARCHIVE attribute, all other attributes will be removed.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.