mfc - How can I dynamically allocate a CFile type of variable? (C++, CFile, new) -
static tchar based_code szfilter[] = _t("yuv files|*.yuv|") cfiledialog filedlg(true, _t("yuv"), _t("bus.yuv"), ofn_filemustexist | ofn_hidereadonly, szfilter); if (filedlg.domodal() == idok) { cstring pathname = filedlg.getpathname(); cfile* pimgfile = null; pimgfile = new cfile(pathname, cfile::moderead || cfile::typebinary); } i refer example in following site. https://msdn.microsoft.com/en-us/library/b569d0t4.aspx
static tchar based_code szfilter[] = _t("yuv files|*.yuv|") cfiledialog filedlg(true, _t("yuv"), _t("bus.yuv"), ofn_filemustexist | ofn_hidereadonly, szfilter); if (filedlg.domodal() == idok) { cstring pathname = filedlg.getpathname(); cfile imgfile; cfileexception e; if (!imgfile.open(pathname, cfile::moderead || cfile::typebinary, &e)) { trace(_t("file not opened %d\n"), e.m_cause); } } i refer first example in following site. https://msdn.microsoft.com/en-us/library/hwbccf8z.aspx
i used method of cfile, open, in second code.
for upper code, how can open file?
when use dynamic allocation, automatically open file?
imglength = pimgfile->getlength(); cstring str; str.format(_t("your system.ini file %i64u bytes long."), imglength); afxmessagebox(str); i tried append code first code.
it works without problems, , think variable, pimgfile, points address of file well.
in general there no need create cfile on heap. there several constructors of cfile:
cfile( ); cfile( catltransactionmanager* ptm ); cfile( handle hfile ); cfile( lpctstr lpszfilename, uint nopenflags ); cfile( lpctstr lpszfilename, uint nopenflags, catltransactionmanager* ptm ); the fourth , fifth constructors call open internally. open file.
the other problem should use operator | not || combine open flags.
i'd suggest using default cfile constructor , call open then. please note optionally can pass cfileexception open call. in case wont throw exception in case of error , wont need place try/catch around it. so, basically, have got more options control behavior if call open manually.
Comments
Post a Comment