Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have used this statements to open the file
CStdioFile readFile;
C++
if(readFile.Open(strFilePath, CFile::modeRead,&fileException))
{
  ...
}

if its already opened i get warning How to check whether the file is already opened?

second time when i try to open the same file I should close the file and read again from first.
Posted
Updated 7-Mar-12 22:15pm
v3
Comments
chaiein 8-Mar-12 22:02pm    
econd time when i try to open the same file i should close the file and read again from first.How to do ??

The Open method should nicely handle such a scenario, that is: the method failure (together with the exception) is a test to see if the file is already open.
 
Share this answer
 
Comments
chaiein 8-Mar-12 3:54am    
But i try to open already opened file exactly near the open statement i am getting Assertion Failure warning. How to solve this problem?
chaiein 8-Mar-12 4:05am    
ya.second time when i try to open the same file i should close the file and read again from first.
chaiein 8-Mar-12 4:14am    
Is there any other way to read a file similar to above without causing problem when i try to open file
I have solve this problem to find whether the file is open or closed by using a variable of boolean type.

C++
if(closed==false)
{
	readFile.Close();
	closed=true;	
	
}	



and before opening a file

C++
if(closed==true)
{
    if (readFile.Open(strFilePath, CFile::modeRead, &fileException))
    {
      closed=false;
      {
        }
    }
}



when ever closed make true when ever opened make false
 
Share this answer
 
Comments
Jochen Arndt 14-Mar-12 7:06am    
So your question is: How to check if the CStdioFile is opened?

You don't need to track it yourself. See CFile::hFileNull [^]
chaiein 14-Mar-12 7:38am    
thanks for the reply I solved my problem using above solution :)
As the debugger showed a protected member of CFile::m_strFileName became empty for the closed file.
So, derive your class from CFile and add a function
bool MyFile::isClosed () const {return m_strFileName.IsEmpty ();} 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900