CFile does not throw exception.

Experts,

I am using CFile class to do file reading and writing operation. I use
the following codes to catch a CFileException when open up a file:

CFile fScr;
CFileException fEx;
if(!fSrc.Open(_T("c:\\1.txt"),CFile::modeRead,&m_fEx))
{
    fEx.ReportError();
    // Then write code to abort operation
}

This code will catch the file access violation exception, meaning that
when the target file is already opened, then CFile::Open(,,,) will
return FALSE and the code will report error.

But, when there is no "c:\1.txt" file exists, the apllication will have
a run time error saying that there is "an unhandled exception". This
means that the above code can not catch all the CFileException during
file openning.

Do you have any idea of making CFile throwing and catch all types of
CFileException?

Thanks,

Baker Huang

0
huangyucbr (13)
6/17/2006 9:57:32 AM
vc.mfc 33608 articles. 0 followers. Follow

4 Replies
1270 Views

Similar Articles

[PageSpeed] 36

Hi Baker,

Only problem i find in the above code is that exception parameter
passed to the file.open is not the correct one,apart from that i dont
see any problem with the code.

-Kiran

Baker wrote:
> Experts,
>
> I am using CFile class to do file reading and writing operation. I use
> the following codes to catch a CFileException when open up a file:
>
> CFile fScr;
> CFileException fEx;
> if(!fSrc.Open(_T("c:\\1.txt"),CFile::modeRead,&m_fEx))
> {
>     fEx.ReportError();
>     // Then write code to abort operation
> }
>
> This code will catch the file access violation exception, meaning that
> when the target file is already opened, then CFile::Open(,,,) will
> return FALSE and the code will report error.
>
> But, when there is no "c:\1.txt" file exists, the apllication will have
> a run time error saying that there is "an unhandled exception". This
> means that the above code can not catch all the CFileException during
> file openning.
>
> Do you have any idea of making CFile throwing and catch all types of
> CFileException?
> 
> Thanks,
> 
> Baker Huang

0
6/19/2006 4:14:10 PM
kiran,

Thank you, but that's not the problem. It's only my typo. It seems that
CFile::Open(..)does not throw an exception for "try - catch" logic.

Best regards,

Baker Huang

kiran.inbng@gmail.com =E5=86=99=E9=81=93=EF=BC=9A

> Hi Baker,
>
> Only problem i find in the above code is that exception parameter
> passed to the file.open is not the correct one,apart from that i dont
> see any problem with the code.
>
> -Kiran
>
> Baker wrote:
> > Experts,
> >
> > I am using CFile class to do file reading and writing operation. I use
> > the following codes to catch a CFileException when open up a file:
> >
> > CFile fScr;
> > CFileException fEx;
> > if(!fSrc.Open(_T("c:\\1.txt"),CFile::modeRead,&m_fEx))
> > {
> >     fEx.ReportError();
> >     // Then write code to abort operation
> > }
> >
> > This code will catch the file access violation exception, meaning that
> > when the target file is already opened, then CFile::Open(,,,) will
> > return FALSE and the code will report error.
> >
> > But, when there is no "c:\1.txt" file exists, the apllication will have
> > a run time error saying that there is "an unhandled exception". This
> > means that the above code can not catch all the CFileException during
> > file openning.
> >
> > Do you have any idea of making CFile throwing and catch all types of
> > CFileException?
> >=20
> > Thanks,
> >=20
> > Baker Huang

0
huangyucbr (13)
6/23/2006 3:32:31 PM
I believe an exception could get thrown (at a level lower than CFile) for 
various file access reasons.

Tom

"Baker" <huangyucbr@yahoo.com.cn> wrote in message 
news:1151076751.049829.293120@m73g2000cwd.googlegroups.com...
kiran,

Thank you, but that's not the problem. It's only my typo. It seems that
CFile::Open(..)does not throw an exception for "try - catch" logic.

Best regards,

Baker Huang

kiran.inbng@gmail.com ??:



0
tserface (3860)
6/23/2006 4:31:09 PM
Use try, catch(...) with the CFile exception set to null:

try
{
   CFile fScr;
   CFileException fEx;
   if(!fSrc.Open(_T("c:\\1.txt"),CFile::modeRead,&fEx))
   {
       fEx.ReportError();
        // Then write code to abort operation
   }
}
catch(...)
{
   // Other error.
   TRACE(_T("Unknown file error."));
}



Baker wrote:
> Experts,
> 
> I am using CFile class to do file reading and writing operation. I use
> the following codes to catch a CFileException when open up a file:
> 
> CFile fScr;
> CFileException fEx;
> if(!fSrc.Open(_T("c:\\1.txt"),CFile::modeRead,&m_fEx))
> {
>     fEx.ReportError();
>     // Then write code to abort operation
> }
> 
> This code will catch the file access violation exception, meaning that
> when the target file is already opened, then CFile::Open(,,,) will
> return FALSE and the code will report error.
> 
> But, when there is no "c:\1.txt" file exists, the apllication will have
> a run time error saying that there is "an unhandled exception". This
> means that the above code can not catch all the CFileException during
> file openning.
> 
> Do you have any idea of making CFile throwing and catch all types of
> CFileException?
> 
> Thanks,
> 
> Baker Huang
> 
0
joe6960 (50)
6/26/2006 9:28:53 AM
Reply:

Similar Artilces:

CFile::Read...
I am working with MFC VC++6 and want to read a file that I created and display the contents of the file to a message box. The file has one character 'a' at position 1. Here is my code: const int MAX_STRING_LENGTH = 10; CString MyString; MyFile.Open("...MyfilePath", CFile::modeNoTruncate | CFile::modeRead ); char* TempBuf = MyString.GetBuffer(MAX_STRING_LENGTH); UINT NumChars = MyFile.Read((void FAR*) TempBuf, MAX_STRING_LENGTH - 1); MyString.ReleaseBuffer(NumChars); AfxMessageBox(MyString); Unfortunately I get an exception error. Does anyone know what I am doing w...

average with exception of some cells?
How can I exclude a few cells in calculating the average, maybe if it has color or whatever the best way to distinguish it from the rest? I can probably manually delete them first, write down the average, revert to previous, but it is not automatic. Thanks, Hi Values in A1:A10, in column B you can mark values to exclude from the calculation with an 'x', and use this formula to calculate the average: =SUMPRODUCT(A1:A10,--(B1:B10<>"x"))/COUNTIF(B1:B10,"<>x") Regards, Per "cpliu" <spamfreeliu@yahoo.com> skrev i meddel...

Office (except Entourage) does not start after Updating to Panther.
> This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. --B_3154584288_141446 Content-type: text/plain; charset="ISO-8859-1" Content-transfer-encoding: 8bit After trying to start any of the office programs under X 10.3, i only get the start-window and the spinning wheel. After a little while i get an error message, the program could not be started. I tried to update to versions 10.1.4 an 10.1.5 (only excel and powerpoint), no effect. I also repaired my disk permissions... I have another office-cop...

help cfile
I have a simple question This is what i have char* pFileName = "C:\\newspaper\\b run\\text.txt"; CFile f( pFileName, CFile::modeCreate | CFile::modeWrite ); loop...... CString out =(buff2+ current->address);<- right here i would like a return f.Write(out,out.GetLength()); ......... f.Close(); what i would like to know is how do i advance to the next line I have tried CString out = (buff2+ current->address+"\n"); well mabe i just have to revert back to the ofstream then cout << endl thanks kevina@ihug.com.au Use "\r\n". tankc &quo...

Formula to show numbers except two
Hi, I have this data in column A that changes each time. A 10 15 I have only the first 2 numbers that go from 10 to 17. I need a formula that from A3 on throws all the numbers from 10 to 17 except the first two. For example, in this case, I have numbers 10 and 15, the formula should show: A 10 15 11 12 13 14 16 17 If I had 12 and 17, it shoul show 10, 11, 13, 14, 15 and 16. Hope my question is clear. Thanks Will the 2 numbers ever be the same: A1 = 17 A2 = 17 Will the the 2 numbers *always* be listed in ascending order? Will this ever be a possi...

delete and exception
I have a problem with a delete and I am trying to know what kind of exception. try { delete [] pArray2Flush; } catch ( ...) { ????? How can I know the exact exception } "mosfet" <tricubes@wanadoo.fr> wrote in message news:c0ipt6$5ab$1@news-reader3.wanadoo.fr... > I have a problem with a delete and I am trying to know what kind of > exception. > > try > { > delete [] pArray2Flush; > } > catch ( ...) > { > ????? How can I know the exact exception > } If you want to write proper C++, the destr...

CFile
hi, i want to read the contents of a text file and to replace the occurance of a particular word. i have used CFile.read and the contents of the file is stored in a character variable,say buffer[100]. now how can i search for a word and replace it. anu Use CStdioFile and read each line into a CString variable. Then use CString:Replace() to find the word and replace. -- Cheers Check Abdoul [ VC++ MVP ] ----------------------------------- "anu" <anu@transversalnet.com> wrote in message news:0c6801c34552$36b6f2a0$a501280a@phx.gbl... > hi, > > i want to read t...

Exception thrown inside AfxWndProc
I've had a dump file that have the following call stack. mfc42u!AfxCallWndProc+0x8b mfc42u!AfxWndProc+0x36 CapPRG!AfxWndProcDllStatic+0x2e .... .... CapPRG!CapPRGStatic::showUp+0x25 CapPRG!CapPRGMainDlg::OnInitDialog+0x7fa Basic what is happening is that inside CapPRGStatic::showUp(), function CWnd::MoveWindow() is called, which generats a window message WM_WINDOWMOVING which is send to AfxWndProcDllStatic for processing. The direct cause of the exception is inside function (MFC4.2) AfxWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam) { // special message which identifies the...

Serialize CFile
Hi, May I know is it possible to serialize a CFile object in CDoc? If so, how can I do it? I have tried to create a CFile m_file in CDoc, then in Serialize, call m_file.Serialize(ar). But it seems like nothing is saved. Pls enlighten, thank you. <lynnzyeo@yahoo.com> wrote in message news:1142763157.155959.302050@v46g2000cwv.googlegroups.com... | Hi, May I know is it possible to serialize a CFile object in CDoc? If | so, how can I do it? I have tried to create a CFile m_file in CDoc, | then in Serialize, call m_file.Serialize(ar). But it seems like nothing | is saved. Pls enlighten, t...

Importing text file into Excel 2003 - C#
Hi all, I am a newby in Excel programming so my question could be an easy one. I found lots of VB code snippets on this issue but almost none in C#. I want to import in a new Excel 2003 doc a character delimited text file. Here is my code: Excel.Workbook lWB = this.excelApp.Workbooks.Add(System.Reflection.Missing.Value); Excel.Worksheet lWS = (Excel.Worksheet)lWB.ActiveSheet; string myfilename = "TEXT;C:\\tmp\\test.txt"; Excel.Range lRange = lWS.get_Range("A1","A1"); /////// the following call generates: System.ArgumentException - The p...

CFile::Read
hi I Open a file using CFile::Open() in read mode. To read from file, we use CFile::Read() If i want to read file in an int type array then how can i do it? suppose CFile myfile; CFileException e; int data[20]; if( !myfile.Open( path, CFile::modeRead ||CFile::shareDenyWrite , &e ) ) { MessageBox(_T("FAILED"));//,NULL,MB_OK); return ; } myfile.Seek(0,CFile::begin ); myfile.Read((TCHAR*)data,20); is this correct? Sachin wrote: >hi >I Open a file using CFile::Open() in read mode. >To read from file, we use CFile::Read() >If i want to read fil...

New exception in IE7 + CWebBrowser2::Navigate()?
Hi everyone! I have a program that uses the IWebBrowser2 ActiveX control, and since I installed IE7 on my system, I've been seeing some new and unwelcome behavior. Specifically, if my program starts when I don't have an internet connection available, my program will display a popup window with the text: Cannot find [URL]. Make sure the path or Internet address is correct. and crash. I can only assume that dialog is coming from IWebBrowser2, since it's not in my code, and it only started showing up after IE7 was installed. The dialog needs to be squelched (I have my own error ...

Exception while exporting data from .net to excel 2003 using sqlse
Hai Application : C#.Net, Windows application BackEnd : SqlServer2000 Excel : Ms-Excel 2003 We have a situation where our data is not exporting from C# windows application to Ms-Excel 2003.we have found that our application cannot load data into excel more than 911 characters in a cell,If i add 1 character more to this group of 911 characters,then i receive this "Exception from HRESULT:0x800A03EC" exception message.so please tell me do u can help me out with any solution,or any patches of microsoft will help us. ...

Archiving: Can archive any folder except the Inbox itself
I am able to select any folder or group of folders in my PST file for archiving, including any and all subfolders *within* the Inbox, but cannot archive the Inbox itself. Attempting to do so raises the single-sentence error, "Error while archiving folder 'Inbox' in store 'Personal Folders.'" I've repaired my PST file multiple times, and everything else works without a problem. Is it possible that I've exceeded some limit for nested folders, or total number of items? The process seems to fail before it even starts. Thank you very much! -Doug Drag/dro...

Exchange 2003 cannot access system Manager (throws MAPI32.dll Q266418 error Popup)
Thank You for reading my post. We have SBS2003 with Exchange Server. For some time now, whenever I attempt to use the Exchagne System Manager, it fails to load, throwing an error dialog box which reads... Exchange System Manager. Exchange detected that "c:\windows\system32=MAPI32.DLL" is not the correct versin required to run Exchange System Manger or Exchange Server 2003. This may cause failures in Exchange System Manager, affect availability of your server, or both. For more information see Microsoft Knowledge Base article Q266418 at http://support/microsoft.com The...

CFile does not throw exception.
Experts, I am using CFile class to do file reading and writing operation. I use the following codes to catch a CFileException when open up a file: CFile fScr; CFileException fEx; if(!fSrc.Open(_T("c:\\1.txt"),CFile::modeRead,&m_fEx)) { fEx.ReportError(); // Then write code to abort operation } This code will catch the file access violation exception, meaning that when the target file is already opened, then CFile::Open(,,,) will return FALSE and the code will report error. But, when there is no "c:\1.txt" file exists, the apllication will have a run time err...

How to refer to a range except one cell
Hi, I'm trying to average a range except one cell in the middle of that range (or alternatively to condition on a range made of two separate sections). The formula I'm trying to use is the following: =IF(AND(B$47>0, B$53=2), B$47-AVERAGEIF(B$45:B$50, ">0"), "-") Except, instead of B45:B50 in AVERAGEIF, I want the range to be B45:B46 and B48:B50 (i.e. without the cell B47). Can anyone tell me how to use a split range with AVERAGEIF or how to tell it not to look at cell B47? Grateful for any advice. Thanks! Just use some helper cells:...

Unhandled Exception
When I try to add a new product, I get the following message: <description>The specified object was not found.</description> I checked to make sure that the description field was there. What could be wrong? Kathyc, Have you made any customizations to the system? Checking the event log on the Event Viewer (on the server) could also help you try to find out what happened. Regards, Leo Lopes @ L3 ...

Word 2007
I know this can be done by header/footer, but I am trying to put the logo on the right side of the page. I can do this manually, basically copy and paste the logo (which I have made using the auto-shape feature), but they don't appear in the exact same location on each page and I have to play with it a lot. Anyway to have this 'logo box' to appear on every page of the report (except for teh first page) automatically? Enable "Different first page" and then anchor the logo to the primary Header (omitting it from the First Page Header). Header content can b...

Having Problems with CFile
Hello I am new at this . But CFile is giving me trouble. I got visual studio 6 and . The CFile is in a function. that starts a file that will be a report file. The program compiles ok, but runs and gives a debug error and crashes help int write_rpt(void) { // begin Report function CFile fileMyFile; char initFile[200]; fileMyFile.Open("c:\rept.html", CFile::modeCreate); fileMyFile.Write(initFile, 15); fileMyFile.Close(); return 0; } Thanks Jim Problem is you didnt give permission to write while opening the file (CFile::modeCreate | CFile::modeWrite). catch th...

CFile::Write
Hi I'm tring to write 3 lines to a text file using CFile::Write. I need a carriage-return between each line but don't seem to be able to achieve that. This is my code CFile mac mac.Open("macro.txt", CFile::modeCreate | CFile::modeWrite) CString st st.Format("CHANNEL 1\n") mac.Write(st, st.GetLength()) st.Format("GRAPHTYPE W\n") mac.Write(st, st.GetLength()) st.Format("LOAD FILE=filename.txt") mac.Write(st, st.GetLength()) mac.Close() when I then open the file with notepad, the carriage return "\n" the carriage return just give me a f...

VB to delete columns except these...
Hello, I have an Excel worksheet called "Sheet1" and I am looking for a way to automatically delete all columns in Sheet1 that are not named "Test1" and Test2". Can someone assist? -- AndrewB. Can you describe what you mean by "columns...not named"? Do you mean "Defined Names"? Or perhaps header text (in Row 1 I presume)? Or did you use the word "named" in a general context meaning those text strings as cell values in some particular (unspecified) column? Or something else entirely? -- Rick (MVP - Excel) &quo...

how do I send email + attachment to several recipients except one
I am using Outlook to send an email with an attachment to several recipients. But one of the recipients does not want to receive the attachment. She only wants the text message but not the file. How do I do this? Thanks (my Outlook version is 2003) You'll need to send a separate email without the attachments. "aml.rofso" <aml.rofso@discussions.microsoft.com> wrote in message news:BCC7B431-0DFF-49F7-A934-E82EEA8A3295@microsoft.com... :I am using Outlook to send an email with an attachment to several recipients. : But one of the recipients does not want to re...

C00D11BA error code no sound except through MagicJack
I lost my internet connection 5pm, called ISP and was told about fiber optics breach, wait 3 hr. decided to watch movie, no sound, tried itunes, then WMP music NO SOUND. At 9:30 back online, no sound. I picked up phone to call ISP and could hear everything through phone during call to ISP and they had no answer so I spent 2 1/2 hrs. with MagicJack live chat to no avail.I had to hold the phone to my head to to watch Keith Olberman and Rachel Maddow, but that ain't gonna work for me. I still get all sound through phone but not computer or speakers. I do not have CMedia AC97 in...

CFile:Read
Hi Team : I want to read a text file, with the next sample code: UINT nActual=0;char szBuffer[1]; CStdioFile file; file.Open("C:/asd1.txt",CFile::modeRead); nActual=file.Read(szBuffer,sizeof(szBuffer)); Only read the first part of the text file, Do you have some tips related to this issue ? Thanks in advanced, German Medina +519-662-8500 Why not just use ReadString instead and read one line at a time into a CString? That's the easiest way to use CStdioFile. Tom "German" <German@discussions.microsoft.com> wrote in message news:D194998E-9CB8-40A0-B220-A9F...