BMorshed


i wrote code to read a text file. The text file was created to the Emulator's \\My Document folder but if i want to open this file is not opening. Actually the open method always return 0.


HANDLE hFile = CreateFile(_T("\\My Documents\\MyTest.txt"),
GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

if (hFile == INVALID_HANDLE_VALUE)
AfxMessageBox(_T("Could not create the file!"));

CFile f;
CFileException e;
TCHAR* pFileName = _T("\\My Documents\\MyTest.txt");
if( !f.Open( pFileName, CFile::modeRead, &e ) )
{
#ifdef _DEBUG
afxDump << "File could not be opened " << e.m_cause << "\n";
#endif

}
Is any file system change to the emulator Actually what is the path of my text file I think my file path is not correct.
Any kind of help is appreciable.

Morshed


Re: Device Emulator General File not found Exception?

Christopher Fairbairn

Hi,

BMorshed wrote:
i wrote code to read a text file. The text file was created to the Emulator's \\My Document folder but if i want to open this file is not opening. Actually the open method always return 0.

The call to CreateFile is creating a file which is shareable by other processes/clients wanting to open the file for reading.

However by default the CFile class will attempt to open the file with exclusive access rights (i.e. it won't share the file with any one else).

Try replacing the line

Code Snippet
if
( !f.Open( pFileName, CFile::modeRead, &e ) )

with the line:

Code Snippet

if( !f.Open( pFileName, CFile::modeRead | CFile::shareDenyNone , &e ) )

Notice the extra flag utilised to state that it is ok to open the file and share it with others. The default without specifying CFile:Tongue TiedhareDenyNone would be CFile:Tongue TiedhareExclusive, i.e. we are requesting exclusive access to the file (which we can not have since there is already an open file handle for the file with write access).

Hope this helps,

Christopher Fairbairn