• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 840
  • Last Modified:

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

0
vishnu_kavalakuntla
Asked:
vishnu_kavalakuntla
  • 3
  • 2
1 Solution
 
ZoppoCommented:
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now