We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

CFile Open Crashing

Medium Priority
1,016 Views
Last Modified: 2013-11-20
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

Comment
Watch Question

CERTIFIED EXPERT
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
In our project specification we should not handle exceptions.It is a single threaded application . Could you suggest us better solution.

Author

Commented:
In our project specification we should not handle exceptions.It is a single threaded application . Could you suggest us better solution.

Author

Commented:
In our project specification we should not handle exceptions.It is a single threaded application . Could you suggest us better solution.
CERTIFIED EXPERT

Commented:
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

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.