file overwrtie

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.
pbsmbcAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DanRollinsCommented:
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! ...
        }
AxterCommented:
Use SetFileAttributes API function before opening the file.
AxterCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pbsmbcAuthor Commented:
Thanks Axter. Its working fine.
I have aquextion.Why FILE_ATTRIBUTE_ARCHIVE attribute is removing read only setting of the file?
AxterCommented:
>>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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.