Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

microsoft ifstream

Status
Not open for further replies.

tokra

Technical User
Feb 20, 2003
45
GB
How do I verify wheather or not a file that i know exists is open by another program. I was thinking of opening a std::ifstream with sharing set to filebuf::sh_none and checking to see if that failed. That is from the old implementation of ifstream though. I was wondering how do I do it with the new implementation (visual c++ 6.0)



WR
 
You can also use CFile with CFile::shareDenyNone|CFile::modeRead for open flags

Matt
 
P.S. That will work provided the file was opened with exclusive access

Matt
 

Is this a file specific to your app that only your app can open? Or, are you talking about any file, excel, word, etc?

A little tip, excel files fail if you try to open them in your app and they are already opened. If you used an extension .xls for your files you can eliminate flag setting when opening.

The code below fail if the .xls file is opened and tells the user. It's opened for output only using ios::eek:ut and deletes it's contents when opened. If I wanted to append I would use ios::app.

ofstream TextFile;
char *cSaveFilePath = "Debug.xls";
TextFile.open(cSaveFilePath, ios::eek:ut);

if(TextFile.fail()){
// check for file open errors
CString csFileError;
csFileError.Format("%s\n\n%s\n\n%s",
"Error opening:",
cSaveFilePath,
"Make sure the file is not in use by another application");
AfxMessageBox(csFileError);
return;
}
 
The purpose of the app watch to monitor a directory for zip files and when one appeared call winzip. However if the file is still beeing written to when winzip is called badness ensues. My logic was to open the file with no sharing and if that succeeded then the file is complete. I solved it with a call to CreateFile

WR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top