CFile Open Crashing

when we do m_FilePointer.Open(m_UsedVBSModuleFileName,CFile::modeCreate  | CFile::modeNoTruncate | CFile::modeWrite ,NULL);  application is crashing.
(we are callin this function multiple times). crash is saying as "The process can not access the file becuase it is being used by another process".

which is happening only in the system testpc,not in the developer pc.

Please suggest us any solution to fix this.



Class a
{
 
public:
void f1();
 
private:
 
CFile m_FilePointer;
}
 
 
a::f1()
{
 
CString stringEnd="\r\n";
 
	m_FilePointer.Open(m_UsedVBSModuleFileName,CFile::modeCreate  | CFile::modeNoTruncate | CFile::modeWrite ,NULL);
	m_FilePointer.SeekToEnd();
	CStringA scriptFileName(scripFilePath);
	CStringA appendString(stringEnd);
	m_FilePointer.Write(scriptFileName,scriptFileName.GetLength());	//to write the contents into the file
	m_FilePointer.Write(appendString,appendString.GetLength());	//to goto the next line
	m_FilePointer.Close();
}
 
when we do m_FilePointer.Open(m_UsedVBSModuleFileName,CFile::modeCreate  | CFile::modeNoTruncate | CFile::modeWrite ,NULL);  application is crashing.
(we are callin this function multiple times). crash is saying as "The process can not access the file becuase it is being used by another process".
 
which is happening only in the system testpc,not in the developer pc.
 
Please suggest us any solution to fix this.

Open in new window

vishnu_kavalakuntlaAsked:
Who is Participating?
 
ZoppoConnect With a Mentor Commented:
Hi vishnu_kavalakuntla,

this might have different reasons, most likely it has something to do with a anti-virus software or any other application which has the file opened.

BTW: you should implement an expection handling around the whole functionality, so you prevent your application from crashing and show a error message or write a log or something else. Something like this:

...
TRY
{
        m_FilePointer.Open(m_UsedVBSModuleFileName,CFile::modeCreate  | CFile::modeNoTruncate | CFile::modeWrite ,NULL);
        ...
        m_FilePointer.Close();
}
CATCH_ALL(e)
{
        m_FilePointer.Abort();
        e->ReportError();
}
END_CATCH_ALL
...

ZOPPO
0
 
vishnu_kavalakuntlaAuthor Commented:
In our project specification we should not handle exceptions.It is a single threaded application . Could you suggest us better solution.
0
 
vishnu_kavalakuntlaAuthor Commented:
In our project specification we should not handle exceptions.It is a single threaded application . Could you suggest us better solution.
0
 
vishnu_kavalakuntlaAuthor Commented:
In our project specification we should not handle exceptions.It is a single threaded application . Could you suggest us better solution.
0
 
ZoppoCommented:
Don't understand - what has exception handling to do with single threaded applications?

BTW: To catch error in CFile::Open you even can pass a pointer to a CFileException and check if TRUE is returned (see attached sample from MSDN).

ZOPPO

BTW: You shouldn't accept a comment as answer with grade 'B' without asking for more/better help in advance ...
CFile f;
CFileException e;
TCHAR* pszFileName = _T("Open_File.dat");
if(!f.Open(pszFileName, CFile::modeCreate | CFile::modeWrite, &e))
{
   TRACE(_T("File could not be opened %d\n"), e.m_cause);
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.