|
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
|
|
|
|
|
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.
|
|
|
|
|
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
|
|
|
|
|
Quick caveat here: filebuf::sh_none is not standard C++. You might end up with problems if you need to compile your code on another compiler.
Cheers,
Ash
|
|
|
|
|
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" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
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!"
|
|
|
|
|
Hello All,
I have a code snippet as follows
struct ItemNode
{
CString szID;
};
void DoSomething()
{
ItemNode *a = NULL;
TRY {
CString s;
s.Format (_T("%s"), a->szID);
}
CATCH(CException b){
MessageBox (_T("Exception occured"));
b->Delete ();
}
END_CATCH
return;
}
Here, I am not able to catch the exception. The app crashes saying "There is an access violation.... "
What is the correct way to catch the exception??
I am using C++, MFC and the development is in VisualStudio 2005.
|
|
|
|
|
Try like this for instance:
void DoSomething()
{
ItemNode *a = NULL;
TRY {
CString s;
s.Format (_T("%s"), a->szID);
}
CATCH(...){
MessageBox (_T("Exception occured"));
}
END_CATCH
}
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Computers are evil, EVIL i tell you!! <
|
|
|
|
|
shouldn't try...catch catch only C++ exceptions? An access violation is a crash, not an exception.
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal
|
|
|
|
|
As far as i know access violation raises an exception, the crash occurs if this exception is nowhere cought in the program itself. I did use try...catch to catch access violations in the past and it worked fine, althorough doing so isn't a very..."nice" solution, but sometimes it can be necesarry.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Computers are evil, EVIL i tell you!! <
|
|
|
|
|
Yeah. I thought, that the CException class should be able to catch all the exceptions.
But in my example, the app crashes without the exception being caught.
Im looking for a way to be able to catch that exception.
|
|
|
|
|
I was talking about the "not nice" part too. That's why I called it crash not exception. IMHO C++ exceptions should be an error reporting mechanism, whereas an access violation means your program just got corrupted.
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal
|
|
|
|
|
True, but he asked how he could catch that exception, that is a way.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Computers are evil, EVIL i tell you!! <
|
|
|
|
|
Tried this, but it is giving errors as "not enough actual parameters for macro 'CATCH'"
|
|
|
|
|
Try this way then
void DoSomething()
{
ItemNode *a = NULL;
try {
CString s;
s.Format (_T("%s"), a->szID);
}
catch(...){
MessageBox (_T("Exception occured"));
}
}
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Computers are evil, EVIL i tell you!! <
|
|
|
|
|
catch( ... ) is generally a really bad idea, it just eats anything and doesn't really tell you a lot. About the only place you should use it is around main so you at least get a hint that something out of the ordinary has occured and you've got a well serious problem:
int main()
try
{
}
catch( std::exception &e )
{
std::cout << "An error occurred: " << e.what() << std::endl;
}
catch( ... )
{
std::cout << "An error occurred: No idea what!" << std::endl;
}
Cheers,
Ash
PS: Using catch( ... ) around thread functions, message handlers and module boundaries can also be good as you don't want to throw through C-style or OS interfaces.
|
|
|
|
|
|
Tried this also...
Still the app shows "Encountered a problem and needs to close..."
Not able to see the messagebox.
|
|
|
|
|
Well, what is MessageBox anyways? IS it your own method? I mean, the MessageBox[^] method in the docs requires more than a string.
Anyways, if i put this in the InitInstance of a test app it displays me the message box as it should.
try {
char *c = NULL;
*c = 3;
} catch (...)
{
AfxMessageBox("Yikes");
}
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Computers are evil, EVIL i tell you!! <
|
|
|
|
|
To catch access violations and such, you need to use Win32 structured exception handling, like __try __except
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal
modified on Friday, May 28, 2010 8:12 AM
|
|
|
|
|
VC++ has a method of converting structured exceptions into C++ exceptions( _se_set_translator() IIRC). It's handy to be able to converge SEH and C++ exceptions into the same handler - provided some muppet doesn't use it as a way to keep code limping along after a null pointer dereference.
Cheers,
Ash
|
|
|
|
|
A small code snippet would be very helpful
Or, can you post how to use the _se_set_translator(), according to the snippet i posted.
|
|
|
|
|
In general catching access violations is the wrong thing to do. If the access violation is coming from inside the TRY block in the code you posted it's more than just "the wrong thing to do", it's sheer madness. Find the real problem! Where exactly in the code is the crash?
Steve
|
|
|
|
|