How to find a file is already opened or not?
-
Hi Everybody, How to find a text file is already opened or not? I have used the following code. but it is not working correctly.
ifstream inFile;
inFile.open("C:\\test.txt");
if (!inFile)
{
AfxMessageBox("file is opened");
}
else
{
AfxMessageBox("file not opened");
}can anyone help me to how to find a file opened or not? Thanks....
G.Paulraj
-
Hi Everybody, How to find a text file is already opened or not? I have used the following code. but it is not working correctly.
ifstream inFile;
inFile.open("C:\\test.txt");
if (!inFile)
{
AfxMessageBox("file is opened");
}
else
{
AfxMessageBox("file not opened");
}can anyone help me to how to find a file opened or not? Thanks....
G.Paulraj
It all depends on how you opened the file. There's no reason why you can't have two streams based on the same file. e.g. on my system:
int main()
{
std::ifstream str1( "c:\\test.txt" );
std::ifstream str2( "c:\\test.txt" );int n = 0; int m = 0; if( ( str1 >> n ) && ( str2 >> m ) ) { std::cout << "Woo hoo! Opened both!" << std::endl; }
}
Always comes up with the "Woo hoo!" message and both n and m are set to the integer that's in the first token of the file. Unfortunately there's no way of opening a file using the standard library that locks it against further reading so if you're using streams you're not going to be able to tell easily if someone else has got it open. Cheers, Ash
-
Hi Everybody, How to find a text file is already opened or not? I have used the following code. but it is not working correctly.
ifstream inFile;
inFile.open("C:\\test.txt");
if (!inFile)
{
AfxMessageBox("file is opened");
}
else
{
AfxMessageBox("file not opened");
}can anyone help me to how to find a file opened or not? Thanks....
G.Paulraj
Paulraj G wrote:
I have used the following code. but it is not working correctly.
What do you mean by not working? Your example shows an open of a single file with no sharing restriction. Please explain what you are trying to do and what results you receive.
It's time for a new signature.
-
Hi Everybody, How to find a text file is already opened or not? I have used the following code. but it is not working correctly.
ifstream inFile;
inFile.open("C:\\test.txt");
if (!inFile)
{
AfxMessageBox("file is opened");
}
else
{
AfxMessageBox("file not opened");
}can anyone help me to how to find a file opened or not? Thanks....
G.Paulraj
Paulraj G wrote:
How to find a text file is already opened or not?
Try opening the file in exclusive mode... filebuf::sh_none If you are unable to open the file exclusively then it means another application has an open handle. Best Wishes, -David Delaune
-
Paulraj G wrote:
How to find a text file is already opened or not?
Try opening the file in exclusive mode... filebuf::sh_none If you are unable to open the file exclusively then it means another application has an open handle. Best Wishes, -David Delaune
-
Hi Everybody, How to find a text file is already opened or not? I have used the following code. but it is not working correctly.
ifstream inFile;
inFile.open("C:\\test.txt");
if (!inFile)
{
AfxMessageBox("file is opened");
}
else
{
AfxMessageBox("file not opened");
}can anyone help me to how to find a file opened or not? Thanks....
G.Paulraj
Paulraj G wrote:
How to find a text file is already opened or not?
It quite difficult, try to open file in write and sharedenyall , if it fails that means somebody else already open the file otherwise none. Preconditon: file must exists at stated path
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
Hi Everybody, How to find a text file is already opened or not? I have used the following code. but it is not working correctly.
ifstream inFile;
inFile.open("C:\\test.txt");
if (!inFile)
{
AfxMessageBox("file is opened");
}
else
{
AfxMessageBox("file not opened");
}can anyone help me to how to find a file opened or not? Thanks....
G.Paulraj
You can use CreateFile Function[] with "OPEN_EXISTING" flag in 5th argument(__in DWORD dwCreationDisposition) and use GetLastError Function to determine f the file opened already or not. Read complete documentation for the above.
-- "Programming is an art that fights back!"